LINQ, XML to tree class
-
I have the following XML: <?xml version="1.0" encoding="utf-8" ?> <string xmlns="http://none.none.none/webservices"> <TREENODES xmlns:sql="urn:schemas-microsoft-com:xml-sql"> <TREENODE code="1" text="None" > <TREENODE code="2" text="America" > <TREENODE code="16128" text="AUTOS"> <TREENODE code="ATO" text="ATO" /> <TREENODE code="ATO" text="ATO" /> </TREENODE> </TREENODE> </TREENODE> <TREENODE code="11" text="None" > <TREENODE code="21" text="America" > <TREENODE code="3" text="AUTOS"> <TREENODE code="ATO44" text="ATO5" /> </TREENODE> </TREENODE> </TREENODE> </TREENODES> </string> And this code to read the xml into class Hierarchy. void client_ReadCompleted(object sender, ReadCompletedEventArgs e) { if (e.Error == null) { string s = e.Result.ToString(); XDocument document = XDocument.Parse(s); var retVal = (from treeNode in document.Descendants("TREENODE") select new Hierarchy() { Code = treeNode.Attribute("code").Value, &n
-
I have the following XML: <?xml version="1.0" encoding="utf-8" ?> <string xmlns="http://none.none.none/webservices"> <TREENODES xmlns:sql="urn:schemas-microsoft-com:xml-sql"> <TREENODE code="1" text="None" > <TREENODE code="2" text="America" > <TREENODE code="16128" text="AUTOS"> <TREENODE code="ATO" text="ATO" /> <TREENODE code="ATO" text="ATO" /> </TREENODE> </TREENODE> </TREENODE> <TREENODE code="11" text="None" > <TREENODE code="21" text="America" > <TREENODE code="3" text="AUTOS"> <TREENODE code="ATO44" text="ATO5" /> </TREENODE> </TREENODE> </TREENODE> </TREENODES> </string> And this code to read the xml into class Hierarchy. void client_ReadCompleted(object sender, ReadCompletedEventArgs e) { if (e.Error == null) { string s = e.Result.ToString(); XDocument document = XDocument.Parse(s); var retVal = (from treeNode in document.Descendants("TREENODE") select new Hierarchy() { Code = treeNode.Attribute("code").Value, &n
FYI: I should have done public static List<Hierarchy> GetHierarchy(XContainer root) { List<Hierarchy> retVal = new List<Hierarchy>(); var children = from treenode in root.Descendants("TREENODE") select treenode; foreach (XNode xn in root.Nodes()) { Hierarchy node = new Hierarchy(); node.Code = ((XElement)xn).Attribute("code").Value.ToString(); node.Text = ((XElement)xn).Attribute("text").Value.ToString(); retVal.Add(node); node.Hierarchies = GetHierarchy((XContainer)xn); } return retVal; }