Link to home
Start Free TrialLog in
Avatar of shwaqar82
shwaqar82

asked on

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
Avatar of QPR
QPR
Flag of New Zealand image

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.
Avatar of shwaqar82
shwaqar82

ASKER

its not the solution ...it wont help me boss
ASKER CERTIFIED SOLUTION
Avatar of satish_nagdev
satish_nagdev
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
thanks anyways..... i fixed it from online example...if you can just tell what is the purpose of normalize in xml