views:

72

answers:

3

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.
+2  A: 
Chris Taylor
Ahh But root was containing reference of biggest ???
Freshblood
Can u please add bigger into biggest without over biggest reference ? I mean just using root referance
Freshblood
@Freshblood, I added an example of what I think you are asking for. Let me know if I misunderstood.
Chris Taylor
@Chris Taylor, I couldn't explain my problem well. I think that XmlLinkedNode class provide what i need.
Freshblood
+3  A: 

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.

Jaroslav Jandek
Sorry for everyone if i couldn't explain my question well.Xml classes get confused me.U you say that u can do samething with XElement i will try it. As result i tried to say that i want to navigate in xml tree and i want to add new element wherever i am in then i want to pass last added element. i was also trying LinqToXml classes.Anyway i will try what u have said. Thanks for your effort
Freshblood
Well, that is exactly what we described. You can save the last added element to a variable and use it later. You are asking very basic programming concepts. Get some books about programming, it will help you more than asking specific questions.
Jaroslav Jandek
A: 

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.

ThaKidd