C Xml Serialize List

Serialize List of Objects. Using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Binary. But I love the speed of C/MFC more so I wanted to create a framework for serializing C objects as XML without resorting to.NET. Well, I have been flirting with the idea of XML serialization for my MFC projects for quite some time. However I never gave it any serious thought. For example, you can serialize a class that simply consists of public fields and properties, as shown in Introducing XML Serialization. The following code examples address various advanced scenarios, including how to use XML serialization to generate an XML stream that conforms to a specific XML Schema (XSD) document. Serializing a DataSet.

  1. The class is of course also serializable, but I have problems attempting to write serialize the file, as it sometimes parses the xml with errors. As my program is quite intensive (writes and reads the xml between pages), I wish to ask if there is any advice you can give on preventing this problem.
  2. Jun 11, 2013  Hello everybody, I need to serialize a list of string text to xml and then desearialze it. List looks like 10, 6, 7, 11 and etc. How should I do this?
Active1 year, 3 months ago

I can't seem to get the XmlSerializer attributes to work. I have

Download WinZip for free – The world's #1 zip file utility to instantly zip or unzip files, share files quickly through email, and much more. Microsoft winzip download free.

What I want is:

C# Xml Serialization List

But what I'm getting is:

[XmlElement(ElementName = 'HardDrives')] on my List<DriveData> does nothing. [XmlElement(ElementName = 'HardDrive')] on my DriveData class causes a build error. How do I set the root and the <HardDrive> elements names to what I want?

Ondrej Tucny
23.6k5 gold badges53 silver badges85 bronze badges
JNygrenLTJNygrenLT

3 Answers

You can keep your model. Use XmlType attribute for the class and second argument as new XmlRootAttribute('HardDrives') while creating serializer object.

L_JL_J
1,9297 gold badges13 silver badges21 bronze badges

While the XmlSerializercan serialize a List<T> directly, like you do with new XmlSerializer(typeof(List<DriveData>)), the trade-off is you can't do much regarding the names of elements produced. A better way is to wrap the list into a class that will be attributed like this:

The [XmlRoot] attribute denotes the top-level document element, while the [XmlElement] Tom & jerry free download video. attribute controls how the property is serialized.

The XML produced by the XmlSerializer will then look like this:

In case you'd embed the List<DriveData> into a more complex structure, the alternate approach is to use [XmlArray] and [XmlArrayItem] attributes, which control how an IEnumerable property is serialized and what elements name is applied to its items. The following data structure:

would then yield an XML like this:

C# Xml Serialize To File

Ondrej Tucny

C# Serialize Xml To Object

Ondrej Tucny

C# Serialize To Xml

23.6k5 gold badges53 silver badges85 bronze badges

There could be much more better ways to achieve this but here is another approach.

C# Xml Serialize To String

Output XML:

KishoreKishore

C# Serialize A List

Not the answer you're looking for? Browse other questions tagged c#xmlxmlserializer or ask your own question.