Note that there are some explanatory texts on larger screens.

plurals
  1. POXml Serialization inside a collection
    primarykey
    data
    text
    <p><code>XmlRoot</code> does not seem to work with classes that are contained within a collection. Here are the classes I have defined:</p> <pre><code>[XmlRoot("cars")] public class CarCollection : Collection&lt;Car&gt; { } [XmlRoot("car")] public class Car { [XmlAttribute("make")] public String Make { get; set; } [XmlAttribute("model")] public String Model { get; set; } } </code></pre> <p>Here is the code I am using to serialize these objects:</p> <pre><code> CarCollection cars = new CarCollection(); cars.Add(new Car { Make = "Ford", Model = "Mustang" }); cars.Add(new Car { Make = "Honda", Model = "Accord" }); cars.Add(new Car { Make = "Toyota", Model = "Tundra" }); using (MemoryStream memoryStream = new MemoryStream()) { XmlSerializer carSerializer = new XmlSerializer(typeof(CarCollection)); carSerializer.Serialize(memoryStream, cars); memoryStream.Position = 0; String xml = null; using (StreamReader reader = new StreamReader(memoryStream)) { xml = reader.ReadToEnd(); reader.Close(); } memoryStream.Close(); } </code></pre> <p>The xml after serialization looks like this:</p> <pre><code>&lt;cars xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Car make="Ford" model="Mustang" /&gt; &lt;Car make="Honda" model="Accord" /&gt; &lt;Car make="Toyota" model="Tundra" /&gt; &lt;/cars&gt; </code></pre> <p>Notice the "C" in car is not lowercase. What do I need to change to make this happen? If I serialize the Car directly it comes out as I would expectg.</p> <p>UPDATE: I found another workaround. I am not sure how much I like it but it will work for my case. If I create a custom class (see below) and have CarCollection derive from it the serialization works as I expected.</p> <pre><code> public class XmlSerializableCollection&lt;T&gt; : Collection&lt;T&gt;, IXmlSerializable { public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { bool wasEmpty = reader.IsEmptyElement; reader.Read(); if (wasEmpty) { return; } XmlSerializer serializer = new XmlSerializer(typeof(T)); while (reader.NodeType != XmlNodeType.EndElement) { T t = (T)serializer.Deserialize(reader); this.Add(t); } if (reader.NodeType == XmlNodeType.EndElement) { reader.ReadEndElement(); } } public void WriteXml(XmlWriter writer) { XmlSerializer reqSerializer = new XmlSerializer(typeof(T)); foreach (T t in this.Items) { reqSerializer.Serialize(writer, t); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload