Wednesday, December 18, 2013

Append an element to an existing XML File (VB.NET)

Here's an example of appending a node to an xml file in VB.NET.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Dim doc As New XmlDocument()  
 Dim file As New FileStream("your_filename", FileMode.Open)  
 doc.Load(file)  
 Try  
      file.Close()  
      file.Dispose()  
      Dim root As XmlNode = doc.DocumentElement  
      Dim el As XmlElement = doc.CreateElement("Invoices", "http://schemas.openxmlformats.org/package/2006/Invoices")  
      el.SetAttribute("Id", "Test")  
      doc.DocumentElement.AppendChild(el)  
      root.InsertBefore(el, doc.DocumentElement.FirstChild)  
      doc.Save("your_filename")  
 Catch ex As Exception  
      Throw ex  
 End Try  

Cheers!

0 comments:

Post a Comment