0

I have an XML Structure that should really have been an array, but is not structured this way. Here is a representation of the XML.

<rootElement>
    <foo>...</foo>
    <bar>...</bar>
    <bar>...</bar>
    <bar>...</bar>
</rootElement>

I cant seem to get <bar> to turn into an array. I have the following C# class to try and do this.

[XmlRoot("rootElement")]
public class MyClass
{
    [XmlArrayItem("bar")]
    public List<MyItems> Items { get; set; }
}

It doesnt work though. I know I could parse the XML using an XML Document and then convert it to a C# object this way but i would rather just use attributes on the C# class and quickly generate the object using XmlSerializer.

I have tried using a variation of attributes on the Items property such as XmlElement and XmlArray but i keep getting 0 items after deserializing. Is there a way this can be done using the XmlSerializer or do i have no other alternative than to use XmlDocument?

Dan Hastings
  • 3,241
  • 7
  • 34
  • 71
  • 1
    I can't answer about using `XmlSerializer`, but if you *do* go the route of manual deserialization, I'd strongly recommend using LINQ to XML (`XDocument` etc) rather than `XmlDocument`. – Jon Skeet Feb 22 '19 at 08:43
  • What does "not working" mean? Please show how you de-serialize your object. I suppose if you don´t have the array as a further nesting in your xml, you should use `XmlElelement` (as for every other property) instead of `XmlArrayItem`. – MakePeaceGreatAgain Feb 22 '19 at 08:45
  • @HimBromBeere read the paragraph after and I explain. "i keep getting 0 items after deserializing" – Dan Hastings Feb 22 '19 at 08:46

0 Answers0