Solved

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

Posted on 2006-11-23
6
668 Views
Last Modified: 2012-05-05
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
Comment
Question by:shwaqar82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 18005438
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
 

Author Comment

by:shwaqar82
ID: 18005594
its not the solution ...it wont help me boss
0
 
LVL 4

Accepted Solution

by:
satish_nagdev earned 500 total points
ID: 18005831
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:shwaqar82
ID: 18007414
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
 
LVL 4

Expert Comment

by:satish_nagdev
ID: 18007553
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
 

Author Comment

by:shwaqar82
ID: 18007722
thanks anyways..... i fixed it from online example...if you can just tell what is the purpose of normalize in xml
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question