0

Hai All i want to create an XML file with following format

<myFiles>
<name>myname</name>
<place>myplace</place>
<mydata>
    <today>1-1-2011</today>
    <time>1 PM</time>
    <driving>
       <car>audi</car>
       <bus>volvo</bus>
    </driving>
</mydata>
<mydata>
    <today>1-1-2011</today>
    <time>1 PM</time>
    <driving>
      <car>audi</car>
      <bus>volvo</bus>             
    </driving>
</mydata>
</myFiles>

For creating this i am using

class myfile
{
public string name {get;set;}
public string place {get;set;}
public list<mydata> mydata {get;set;}
}

then also public claa mydata { ...... } fro XML building i am using

static public void SerializeToXMLList(List<Movie> movies)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Movie>));
TextWriter textWriter = new StreamWriter(@"C:\movie.xml");
serializer.Serialize(textWriter, movies);
textWriter.Close();
}

But it will give me output like

<myFiles>
<name>myname</name>
<place>myplace</place>
<mydata>
   <mydata>
   <today>1-1-2011</today>
   <time>1 PM</time>
   <driving>
     <car>audi</car>
     <bus>volvo</bus>
   </driving>
   </mydata>
   <mydata>
   <today>1-1-2011</today>
   <time>1 PM</time>
   <driving>
      <car>audi</car>
      <bus>volvo</bus>
   </driving>
</mydata>
</mydata>
</myFiles>

so there was number of list with in mydata list with class name

How can i avoid that..., If any one knw pls help me..

2 Answers 2

2

The reason for that is your have a property called 'mydata', and the objects within it are of type 'mydata'.

It's actually good practice to contain an array in an element like this so to be honest I think the layout you're getting is better, although you should probably choose a better name for the property, such as 'mydatacollection' or 'mydatalist'.

However, there's more information here on the attributes you can use to control the serialization: http://msdn.microsoft.com/en-us/library/2baksw0z%28v=vs.80%29.aspx#Y570

@Totero posted a link to another good question on this subject if you really do want to do this.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes i am agreed with you that is the best methode. But i cannot do that. [XmlArrayItem("mydaa")] will again create same result. But i want to avoid that. Is it possible
1

The reason it is creating nested containers is becuase you have an array mydata of mydata objects.

<mydata>
   <mydata>
   ....
   </mydata>
</mydata>

is the correct way of representing this. The quickest way to get the file in the exact format you want is to rename the array to something like "EraseTag". Open the XML file as a text file and remove all instances of "<EraseTag>".

However I want to reiterate the 2nd serialized example you gave is correct and as such can be reverted back to the object form by deserialization.

If you want to leave these tags out of the serialisation phase then you need to look at the following post:

XML Deserialization and Loose Array Items

and add a [Serializable] tag around your class.

2 Comments

Yes Ok i agree with you but i need the XML in firstFormat(first Eg in above) at the time of creating. I think i am using list that why separate list within a list is created but how can i avoid that
Updated answer. There is another stack overflow item on this topic already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.