Add Node to XML

bortiquai

Member
Joined
Jul 5, 2007
Messages
17
Programming Experience
Beginner
i have an XML file that is a mile long, and I need to add an element, but it is near the top.
If you were to look at the XML, it would go something like:

HTML:
<VeryTop> - if i colapse this one, everthing is inside of it.
    <NextToTop>  -if i colapse this one, most things are inside
          <WhereINeedToInsert>---I need to insert this node
                  <someValuesToInsert/> --as well as some values
          </WhereINeedToInsert>
     </NextToTop>
</VeryTop>

i am loading the XML file with XPathNavigaor with the following Code:

VB.NET:
            Dim xpathDoc As XPathDocument
            Dim xmlNav As XPathNavigator
            xpathDoc = New XPathDocument("tempXML")
            xmlNav = xpathDoc.CreateNavigator()

But I can't seem to figure out how to insert what I need.
Thanks
 
Last edited by a moderator:
You use the Move/Select... methods of the navigator to go to where you want to work. The Append/Insert/Prepend... methods adds data. Here is one of many alternative ways:
VB.NET:
    Sub navedit()
        Dim document As New Xml.XmlDocument
        document.Load("xpathnav.xml")
        Dim nav As Xml.XPath.XPathNavigator = document.CreateNavigator
        nav = nav.SelectSingleNode("/VeryTop/NextToTop")
        nav.AppendChildElement("", "WhereINeedToInsert", "", "")
        nav.MoveToFirstChild()
        nav.AppendChildElement("", "someValuesToInsert", "", "")
        document.Save("xpathnav2.xml")
    End Sub
 
Thanks for your reply.

I tried the code you suggested, but it throws an exception:
Object reference not set to an instance of an object.

When i mouse-over on "nav" after the line:

VB.NET:
        Nav.AppendChildElement("", "/VeryTop/NextToTop", "", "")

i can see that "Nav" is nothing after it steps over this line.

if you were to open the xml in notepad, it would look like:
HTML:
<VeryTop><NextToTop></NextToTop></VeryTop>

I need the result:
HTML:
<VeryTop><NextToTop><WhereINeedToInsert><someValuesToInsert Value='1'/></WhereINeedToInsert></NextToTop></VeryTop>

Thanks again
 
Last edited by a moderator:
That's what the code does when I run it, ie the first xml you posted is the starting document and the latter the result.
 
Indeed...how embarrassing. I'm not sure what i did the first time, but i've got it now. Thank you very much for your help.

One final thing:
when i run the code, it puts the new Element after the existing one:
so if i started with:
HTML:
<VeryTop>
    <NextToTop>
    </NextToTop>
</veryTop>

and i want to insert an element under "VeryTop" (a sibling of "NextToTop"?) it places it after "NextToTop", so i get:
HTML:
<VeryTop>
   <NextToTop>
   </NextToTop>
   <WhatIInsert>
   </WhatIInsert>
</VeryTop>
This is an over simplified example, but in a very large XML file, does the order of these matter? I've tried a few different combinations to try to get:
HTML:
<VeryTop>
   <WhatIInsert>
   </WhatIInsert>
   <NextToTop>
   </NextToTop>
</VeryTop>

But I can't seem to get it - either it throws an exception or "WhatIInsert" always comes after.
Thanks again
 
Last edited by a moderator:
This is an over simplified example, but in a very large XML file, does the order of these matter?

I've tried a few different combinations to try to get:
HTML:
<VeryTop>
   <WhatIInsert>
   </WhatIInsert>
   <NextToTop>
   </NextToTop>
</VeryTop>

But I can't seem to get it - either it throws an exception or "WhatIInsert" always comes after.
Thanks again
The order of siblings with different element names only matter if the document has schema that rules this and the document is validated against this.

Do you really "insert", or "append" like previous code? Appending is adding after, inserting is putting before. I would select the same xpath node as before, the "NextToTop" node, and use the InsertElementBefore method.
 
Back
Top