Consider the following XML:
<?xml version="1.0" encoding="utf-8"?> <treelist id="foo" displayname="display"> <treelink id="link" /> </treelist>
I have the following code:
private static void Main(string[] args) { StreamReader result = File.OpenText(@"test.xml"); var xmlTextReader = new XmlTextReader(result.BaseStream, XmlNodeType.Document, null); XDocument load = XDocument.Load(xmlTextReader); var xmlSerializer = new XmlSerializer(typeof (TreeList)); var foo = (TreeList) xmlSerializer.Deserialize(load.CreateReader()); }
And these are my entities:
[Serializable] [XmlRoot("treelink")] public class TreeLink { [XmlAttribute("id")] public string Id { get; set; } } [Serializable] [XmlRoot("treelist")] public class TreeList { [XmlAttribute("id")] public string Id { get; set; } [XmlAttribute("displayname")] public string DisplayName { get; set; } [XmlArray("treelist")] [XmlArrayItem("treelist", typeof (TreeLink))] public TreeLink[] TreeLinks { get; set; } }
However, I cannot deserialize treelink objects, in foo TreeLinks always remains null.
What am I doing wrong here?
thanks
Snake source share