Jagd
Jagd

Reputation: 7306

Serializing object to XML

I have a class in C# setup that serializes itself to XML, and this class has a List of objects that it serializes as well.

[XmlRoot("Config")]
public class ConfigSerializer {

    [XmlArray("Nodes")]
    public List<Node> LstNodes { get; set; }  

}

And here is the class declaration for Node.

[XmlRoot("N")]
public class Node {
  // has a few different properties
}

PROBLEM: When I serialize an instance of ConfigSerializer to XML it doesn't serialize the XML as I would expect it to with regards to the Node list. It looks like this...

<Config>
  <Nodes>
    <Node></Node>
  </Nodes>
</Config>

But I would expect it to look like this (because of the XmlRoot declaration for the Node class)...

<Config>
  <Nodes>
    <N></N>
  </Nodes>
</Config>

Upvotes: 1

Views: 99

Answers (1)

juliohuang
juliohuang

Reputation: 56

You can use this

    [XmlRoot("Config")]
    public class ConfigSerializer
    {
        [XmlArray("Nodes"),XmlArrayItem("N")]
        public List<Node> LstNodes { get; set; }
    }

Upvotes: 4

Related Questions