Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XML to ObservableCollection

XML to ObservableCollection

Scheduled Pinned Locked Moved C#
questiondatabasecomdata-structuresxml
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VickyC
    wrote on last edited by
    #1

    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>

    J 1 Reply Last reply
    0
    • V VickyC

      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>

      J Offline
      J Offline
      Jeremy Likness
      wrote on last edited by
      #2

      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/

      V 2 Replies Last reply
      0
      • J Jeremy Likness

        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/

        V Offline
        V Offline
        VickyC
        wrote on last edited by
        #3

        So what would be the LINQ? Thanks in Advance

        1 Reply Last reply
        0
        • J Jeremy Likness

          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/

          V Offline
          V Offline
          VickyC
          wrote on last edited by
          #4

          Also this class it seems that will create a simple list. Not a tree like the XML.

          J 1 Reply Last reply
          0
          • V VickyC

            Also this class it seems that will create a simple list. Not a tree like the XML.

            J Offline
            J Offline
            Jeremy Likness
            wrote on last edited by
            #5

            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/

            V 2 Replies Last reply
            0
            • J Jeremy Likness

              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/

              V Offline
              V Offline
              VickyC
              wrote on last edited by
              #6

              Have you seen the Linq exception "Sequence contains more than one element"? It comes from Populate.

              1 Reply Last reply
              0
              • J Jeremy Likness

                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/

                V Offline
                V Offline
                VickyC
                wrote on last edited by
                #7

                it was the FirstOrDefault not SingleOrDefault. Will this work if I change the class from List to ObservableCollection? Thanks a bunch.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups