How do I insert a XML node into a XML document?

XmlNode cNode;  // node after which a new node will be inserted
XmlDocument xdoc = new XmlDocument(); 
//create and load an xml doc
xdoc.LoadXml("<XMLFile>" + “<OldData>Previous Data</OldData>" + 
"</XMLFile>"); 
XmlDocumentFragment docFragment = doc.CreateDocumentFragment(); 
docFragment.InnerXml = "<Inserted>" + " <NewData>Inserted Data</NewData>" + "</Inserted>"; 
// insert the new node into the document created above
cNode = xdoc.DocumentElement.FirstChild; 
cNode.InsertAfter(docFragment, cNode.LastChild); 
//save the resultant xml doc to a file 
xdoc.Save("NewDoc.xml"); 


Back to XML FAQ



 
Printer friendly version of the FAQ-XML-Insert-Node-Into-A-Document page


Sponsored links

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Web based bug tracking - AdminiTrack.com
AdminiTrack offers an effective web-based bug tracking system designed for professional software development teams.
Free White Paper: Accelerate Enterprise Apps 2500%
Sign up to learn how solid state disks boost application speed for lots more transactions and users.
Free DB Modeling Trial with ER/Studio
Design and Build More Powerful Databases with ER/Studio.

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.