Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / January 2008

Tip: Looking for answers? Try searching our database.

linq nested xml

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Guzeppi - 26 Jan 2008 12:10 GMT
Hi,

i'm using linq to load an xml structure into my classes. the xml consists of
the same node nested for multiple levels e.g.

<node id="node_id01" name="node 01">
   <node id="node_id0101" name="node 01 01">
       <node id="node_id010101" name="node 01 01 01">
           <node id="node_id01010101" name="node 01 01 01 01">
               <node id="node_id0101010101" name="node 01 01 01 01 01">
               </node>
           </node>
       </node>
   </node>
</node>

The class consits of the properties which map to the xml attributes and has
a children property and a parent property.

Using recursion i am able to constract the class tree from parent to
children, however i'm not able to link up the parent with the children.
the code i'm using is as follows:

private List<NodeClass> GetNodes(XElement xmlelement, bool enabledOnly, int
level )
{
   level++;
   var elementsQuery = from element in xmlelement.Elements("node")
                                   select new NodeClass{
                                       Id = element.Attribute("id").Value,
                                       Name =
element.Attribute("name").Value,
                                       Level = level,
                                       //Parent = ?????
                                       Children = GetNodes(element,
enabledOnly, level)
                                   };
   return elementsQuery.ToList();
}

the parent property is of type NodeClass and i'd like it to be the parent of
the child node or null when level is 0. any suggestments please?
Martin Honnen - 26 Jan 2008 14:32 GMT
> The class consits of the properties which map to the xml attributes and has
> a children property and a parent property.
[quoted text clipped - 22 lines]
> the parent property is of type NodeClass and i'd like it to be the parent of
> the child node or null when level is 0. any suggestments please?

Well you need to pass the parent NodeClass instance to that method e.g.

    class NodeClass
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public int Level { get; set; }
        public NodeClass Parent { get; set; }
        public List<NodeClass> Children { get; set; }

        NodeClass() { }
        public NodeClass(XElement nodeElement)
        {
            Id = nodeElement.Attribute("id").Value;
            Name = nodeElement.Attribute("name").Value;
            Level = 0;
            Parent = null;
            Children = GetNodes(nodeElement, Level, this);
        }

        private List<NodeClass> GetNodes(XElement nodeElement, int
level, NodeClass parent)
        {
            level++;
            var elementsQuery =
                from childElement in nodeElement.Elements("node")
                select new NodeClass {
                    Id = childElement.Attribute("id").Value,
                    Name = childElement.Attribute("name").Value,
                    Level = level,
                    Parent = parent,
                    Children = GetNodes(childElement, level, this)
                };
            return elementsQuery.ToList();
        }
    }

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.