I'm having issues deserializing XML which I can't quite figure out. Mainly, I'm trying to avoid having to nest classes when they're inside a list but not succeeding. For example:
[XmlRoot]
[Serializable]
public class Foo
{
[XmlElement("Bar")]
public BarElement Bar = new BarElement();
public class BarElement
{
[XmlElement("MoreBars")]
public List<MoreElement> MoreBars = new List<MoreElement>();
}
[XmlRoot("More")]
[Serializable]
public class MoreElement
{
[XmlAttribute("Attribute")]
public string Attribute { get; set; }
[XmlText]
public string Value { get; set; }
}
}
Corresponds to:
<Foo>
<Bar>
<MoreBars>
<More Attribute=""></More>
<More Attribute=""></More>
<More Attribute=""></More>
<More Attribute=""></More>
</MoreBars>
</Bar>
</Foo>
This almost works... but not quite. By adding XmlRoot to MoreElement, I'm trying to avoid the necessity of having to create a new class named "MoreBarsElement" that contains only a List of MoreElements, since it's already quite a chore to access "Foo.Bar.MoreBars.Value". Is this possible? If so, how would I do it?
Answering my own question - looks like I needed
[XmlArray("MoreBars"), XmlArrayItem(typeof(MoreElement), ElementName = "More")]
On the array item declaration. It works now, hurray!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.