1

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>

1 Answer 1

4

You can use this

    [XmlRoot("Config")]
    public class ConfigSerializer
    {
        [XmlArray("Nodes"),XmlArrayItem("N")]
        public List<Node> LstNodes { get; set; }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

XmlArray Attribute is the solution for sure, +1

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.