linq xml nexted node parent

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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 child.
the code i'm using is as follows:

private List<NavigationElement> 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 = parentElement,
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 subbestments please?


.



Relevant Pages

  • linq nested xml
    ... i'm using linq to load an xml structure into my classes. ... a children property and a parent property. ... Using recursion i am able to constract the class tree from parent to ... the parent property is of type NodeClass and i'd like it to be the parent of ...
    (microsoft.public.dotnet.xml)
  • Re: Insert large amounts of data
    ... Most performance because you special case the processing to the XML format ... > Elements per Parent Element. ... There is also usually one Parent element ... > fastest with an estimated operator cost of 2.645. ...
    (microsoft.public.sqlserver.xml)
  • Re: FOR XML EXPLICIT cant get element 4 going
    ... This was about driving me nuts, but I don't do FOR XML that often. ... SELECT EmployeeID AS, ... that each parent node is immediately followed by its children. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ...
    (comp.databases.ms-sqlserver)
  • Re: AppendChild removes like nodes?
    ... | The interface allows a user to manipulate an XML document. ... | parent information from the item being pasted to, ... | says "If the newChild is already in the tree, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Storing hierarchies from XML into relational tables
    ... You haven't made clear what sort of relational tables you plan to use to store the data from your hierarchy. ... Parent VARCHAR ... Given an xml document that represents a heirarchy: ...
    (microsoft.public.sqlserver.xml)