• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 672
  • Last Modified:

how do i append new xml node to a existing xml file

Hi

I wana add the new node at the end of all the nodes in an existing xml file. Let say i have special.xml file which has following nodes :

<HOTELS>
<HOTEL><DEST>Toronto</DEST></HTOEL>
<HOTEL><DEST>Alberta</DEST></HTOEL>
<HOTEL><DEST>London</DEST></HTOEL>
<HOTEL><DEST>Calgary</DEST></HTOEL>
</HOTELS>


i need to add the new node at the end and before </HOTELS> root element. I need to add the new <HOTEL> node with new <DEST> child node in it. I have a function named AddNode(). It has strNode variable in it. Just need to add the value of strNode in xml file. This function is already being called by button click event
HELP ME PLZ.

Best Regards
Shaukat
0
shwaqar82
Asked:
shwaqar82
  • 3
  • 2
1 Solution
 
QPRCommented:
How about loading the xml file into a xml reader or string (excuse the lack of xml in .net knowledge) and then treat it as a large string?
myNewXML = myOldXML + myNewNode + '</HOTELS>

myOldXML would be the contents of the xml file minus the right 8 characters (</HOTELS>)
Probably a cleaner way using XPath or something but it's something to consider while waiting for the XML gurus to show up.
0
 
shwaqar82Author Commented:
its not the solution ...it wont help me boss
0
 
satish_nagdevCommented:
hi Waqar,
try this please
        XmlDocument xd = new XmlDocument();
        xd.Load("C:\\data.xml");
        XPathNavigator xn = xd.CreateNavigator();
        xn.MoveToFirstChild();
        xn.AppendChild("<HOTEL><DEST>India</DEST></HOTEL>");
        xd.Save("C:\\data.xml");

regards,
satish.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
shwaqar82Author Commented:
thats frustrating no one is answering me ..its kind of simple question but i cant find the solution coz im doing it first time.
Satish

i tried your code but its not working nothin gis added in the xml file.
My question is HOW TO ADD NEW NODE TO AN EXISTING XML
0
 
satish_nagdevCommented:
hi waqar,
sorry but the code i gave you is working perfect on my system.
what i did is created a xml file in my c-drive, copied your data that you've post actually, i got error coz there was spelling mistel HOTEL & HTOEL after that i got it working very fine.

you want to append xml node to xml in file or variable like string etc.?

regards,
satish.
0
 
shwaqar82Author Commented:
thanks anyways..... i fixed it from online example...if you can just tell what is the purpose of normalize in xml
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now