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>
<Bigg开发者_如何学运维est>
<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:
XElement root = new XElement("root",
new XElement("first", "value",
new XElement("second", "Value")));
Traverse the nodes using various methods like Element
:
root.Element("first").Element("second").Add(new XElement("third", "Value"));
//or
root.Descendants("second").First().Add(new XElement("underTheThird", "Value"));
Add to any element:
root.Add(new XElement("underTheSecond", 456));
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 do first.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 an XText
(string, numbers, ... - primarily using XmlConvert
).
Freshblood: I think that XmlLinkedNode class provide what i need.:
You can use either the NextNode
or the PreviousNode
property to get the siblings and the Parent
property to get the parent of the current node.
Although all that info is reachable through the link I have posted in my answer.
If I understand correctly, you want to create XML elements and then comeback and add addtional child elements. You might want you look at using the XmLDocument class for this.
Using XElement you can also do this as follows
XElement root = new XElement("Root");
XElement biggest = new XElement("Biggest");
XElement bigger = new XElement("Bigger");
XElement another = new XElement("Another");
root.Add(biggest);
biggest.Add(bigger);
biggest.Add(another);
Here each step adds to the hierarchy, but at each interim step the hierarchy is valid XML.
Update: Based on the question in the comment I think this is what you are looking for
root.Add(biggest);
root.Element("Biggest").Add(bigger);
root.Element("Biggest").Add(another);
Don't know if it helps, but I found this guys' site very helpful to work with xml files.
http://www.codeproject.com/KB/XML/XML_Configuration_File.aspx
If you use it, make sure you wrap all of the instances in using..found that out the hard way.
精彩评论