XML to ObservableCollection
-
How can I load an XML Document to an ObservableCollection class that preserves the XML tree structure? My xml looks like this: <?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>
-
How can I load an XML Document to an ObservableCollection class that preserves the XML tree structure? My xml looks like this: <?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>
Depends on what you're loading it for. What is the consumer of the object? You can make something like:
public class TreeNode
{
public string Code { get; set; }
public string Text { get; set; }
public List Children { get; set }public TreeNode() { Children = new List(); }
}
In both WPF and Silverlight you could databind with a hierarchal template. Again, it all depends on how/what/why you are using it. You can populate the tree using LINQ to XML.
Jeremy Likness http://csharperimage.jeremylikness.com/
-
Depends on what you're loading it for. What is the consumer of the object? You can make something like:
public class TreeNode
{
public string Code { get; set; }
public string Text { get; set; }
public List Children { get; set }public TreeNode() { Children = new List(); }
}
In both WPF and Silverlight you could databind with a hierarchal template. Again, it all depends on how/what/why you are using it. You can populate the tree using LINQ to XML.
Jeremy Likness http://csharperimage.jeremylikness.com/
-
Depends on what you're loading it for. What is the consumer of the object? You can make something like:
public class TreeNode
{
public string Code { get; set; }
public string Text { get; set; }
public List Children { get; set }public TreeNode() { Children = new List(); }
}
In both WPF and Silverlight you could databind with a hierarchal template. Again, it all depends on how/what/why you are using it. You can populate the tree using LINQ to XML.
Jeremy Likness http://csharperimage.jeremylikness.com/
-
It will create a hierarchal object graph. Something like this would populate it:
public TreeNode Populate(XDocument doc)
{
var retVal = (from treeNode in doc.Descendants("TreeNode")
select new TreeNode()
{
Code = treeNode.Attribute("Code").Value,
Text = treeNode.Attribute("Text").Value
}).SingleOrDefault();
retVal.Children = _Recurse((from treeNode in doc.Descendants("TreeNode")
select treeNode).SingleOrDefault());
return retVal;
}private static List<TreeNode> _Recurse(XContainer root)
{
List<TreeNode> retVal = new List<TreeNode>();
var children = from child in root.Descendants("TreeNode") select child;
foreach(var child in children)
{
TreeNode node = new TreeNode
{
Code = child.Attribute("Code").Value,
Text = child.Attribute("Text").Value,
Children = _Recurse(child)
};
retVal.Add(node);
}
return retVal;
}Jeremy Likness http://csharperimage.jeremylikness.com/
-
It will create a hierarchal object graph. Something like this would populate it:
public TreeNode Populate(XDocument doc)
{
var retVal = (from treeNode in doc.Descendants("TreeNode")
select new TreeNode()
{
Code = treeNode.Attribute("Code").Value,
Text = treeNode.Attribute("Text").Value
}).SingleOrDefault();
retVal.Children = _Recurse((from treeNode in doc.Descendants("TreeNode")
select treeNode).SingleOrDefault());
return retVal;
}private static List<TreeNode> _Recurse(XContainer root)
{
List<TreeNode> retVal = new List<TreeNode>();
var children = from child in root.Descendants("TreeNode") select child;
foreach(var child in children)
{
TreeNode node = new TreeNode
{
Code = child.Attribute("Code").Value,
Text = child.Attribute("Text").Value,
Children = _Recurse(child)
};
retVal.Add(node);
}
return retVal;
}Jeremy Likness http://csharperimage.jeremylikness.com/
-
It will create a hierarchal object graph. Something like this would populate it:
public TreeNode Populate(XDocument doc)
{
var retVal = (from treeNode in doc.Descendants("TreeNode")
select new TreeNode()
{
Code = treeNode.Attribute("Code").Value,
Text = treeNode.Attribute("Text").Value
}).SingleOrDefault();
retVal.Children = _Recurse((from treeNode in doc.Descendants("TreeNode")
select treeNode).SingleOrDefault());
return retVal;
}private static List<TreeNode> _Recurse(XContainer root)
{
List<TreeNode> retVal = new List<TreeNode>();
var children = from child in root.Descendants("TreeNode") select child;
foreach(var child in children)
{
TreeNode node = new TreeNode
{
Code = child.Attribute("Code").Value,
Text = child.Attribute("Text").Value,
Children = _Recurse(child)
};
retVal.Add(node);
}
return retVal;
}Jeremy Likness http://csharperimage.jeremylikness.com/