Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting an XML Node

Status
Not open for further replies.

emblewembl

Programmer
May 16, 2002
171
GB
I want to insert a new xml childNode into my document as marked below:

Code:
 <division name="Get Course">
    <section default="true">
      <conditions>
      </conditions>
      <statements>
        <s>@ename@ has run @Runners@ times </s>
        <s>After @Runners@ races, won @winners@ times</s>
      </statements>
    </section>
    
    <section>
      <conditions>
        [b]<condition>New node here</condition>[/b]
        <condition>@Runners@ more than 5</condition>
      </conditions>
      <statements>
        <s>Run more than @Runners@ times</s>
        <s>out of @Runners@ runs</s>
      </statements>
    </section>
  </division>

I want to be able to add new <condition></condition> statements in the <conditions></conditions> node but cannot find how to do this. Can anyone help?


i love chocolate
 
What language/environment are you using? .Net? ASP? Java?

Jon

"I don't regret this, but I both rue and lament it.
 
Sorry! I'm using c# in a windows form.

i love chocolate
 
Well I assume that you already have the XML tree in memory?

Create a new XMLNode with teh appropriate settings

Navifate the XML tree to the appropriate place
Add the Node to the Parent. (i.e. in your example you would add your node to the child collection of <conditions>
 
How are you choosing which section you want? I have presumed you want the second one. Use XPath to select the node, then create a new node and prepend it:
Code:
XmlDocument doc = new XmlDocument();
doc.Load("MyXML.xml")

XmlNode conditions = doc.SelectSingleNode("division/section[2]/conditions");

//Create a new condition.
XmlElement condition= doc.CreateElement("condition");
condition.InnerText = "node value";

//Add the condition.
conditions.PrependChild(condition);
Look at System.Xml.XmlNode and System.Xml in general:


Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top