?
Solved

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

Posted on 2006-11-23
6
Medium Priority
?
669 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 2000 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
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!

 

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

Industry Leaders: 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!

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 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