I want to create dynamic XML hieararchy but still couldn’t be successful about it.I tried LinqToXml classes and XML classes but still i couldn’t. I want to create such a hierarchy in a loop dynamically.
Create root node
<Root></Root>
Then add child for it.
<Root>
<Biggest></Biggest>
</Root>
Then add child for last added node
<Root>
<Biggest>
<Bigger>
</Bigger>
</Biggest>
</Root>
Then go back add another
<Root>
<Biggest>
<Bigger>
</Bigger>
<Another>
</Another>
</Biggest>
</Root>
Edit: i want to give an example.
XElement root = new XElement("root");
XElement first = new XElement("first", "value");
XElement second = new XElement("second", "Value");
root.Add(first);
//now how can add second node into first ??
//I don't want to add second one into first then add it into root.
You can create xml structure directly:
Traverse the nodes using various methods like
Element:Add to any element:
etc.
Microsoft has several documents available for reading – like the Reference (LINQ to XML).
Edit – collected the info I’ve posted in comments:
You can do
root.Add(first);first and then dofirst.Add(second);. The order in which you do that does not matter. The Xml document is not built like a string, it is a hierarchy of objects – you can add new nodes anywhere in the tree.Add uses an object as a parameter (same as the element constructor). You can add any
XObject(that is possible to add) and any other object convertable to anXText(string, numbers, … – primarily usingXmlConvert).Freshblood: I think that XmlLinkedNode class provide what i need.:
You can use either the
NextNodeor thePreviousNodeproperty to get the siblings and theParentproperty to get the parent of the current node.Although all that info is reachable through the link I have posted in my answer.