Solved

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

Posted on 2006-11-23
6
653 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
  • 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now