0% found this document useful (0 votes)
46 views

Inserting Data To An XML Document

The XmlNode and XmlDocument classes can be used to insert XML data into an existing XML document or create a new one. The document can be loaded using the LoadXml method which takes an XML string. New data can then be inserted into the document by creating a DocumentFragment with the new XML, getting a reference node, and using InsertAfter to add it after the last child of the reference node. The updated document is then saved to a new file.

Uploaded by

Ajantha Devi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Inserting Data To An XML Document

The XmlNode and XmlDocument classes can be used to insert XML data into an existing XML document or create a new one. The document can be loaded using the LoadXml method which takes an XML string. New data can then be inserted into the document by creating a DocumentFragment with the new XML, getting a reference node, and using InsertAfter to add it after the last child of the reference node. The updated document is then saved to a new file.

Uploaded by

Ajantha Devi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Inserting Data to an XML Document

The XmlNode and the XmlDocument classes can be used to insert XML data to an existing document or to a new document. Adding namspace Reference Since Xml classes are defined in the System.XML namespace, so first thing you need to do is to Add the System.XML reference to the project. Imports System.Xml Loading XML to Document LoadXml method of XmlDocument can be used to load XML data to a document or to load an existing XML document.. ' Load XML data to a document Dim doc as new XmlDocument(); doc.LoadXml("<XMLFile>" + " <SomeData>Old Data</SomeData>" + "</XMLFile>") Inserting XML Data The below code inserts XML data to the file and saves file as InsertedDoc.xml. Souce Code: Try Dim currNode As XmlNode Dim doc As New XmlDocument() doc.LoadXml(("<XMLFile>" + " <SomeData>Old Data</SomeData>" + "</XMLFile>")) Dim docFrag As XmlDocumentFragment = doc.CreateDocumentFragment() docFrag.InnerXml = "<Inserted>" + " <NewData>Inserted Data</NewData>" + "</Inserted>" ' insert the availability node into the documentcurrNode = doc.DocumentElement.FirstChild; currNode.InsertAfter(docFrag, currNode.LastChild) 'save the output to a filedoc.Save("InsertedDoc.xml"); Catch e As Exception Console.WriteLine("Exception: {0}", e.ToString()) End Try

The output of the code looks like this

<XMLFile> <SomeData> Old Data <Inserted> <NewData>Inserted Data</NewData> </Inserted> </SomeData> </XMLFile>

You might also like