Append data to an XML file (VB.net) : XMLDocument « XML « ASP.NET Tutorial






<%@ Page Language="VB" %>
<%@ Import Namespace="System.Xml" %>

<script runat=server>
   sub Page_Load(Sender as Object, e as EventArgs)
      dim xmldoc as new XMLDocument()
         
      try
         xmldoc.Load(Server.MapPath("Data.xml"))
         dim eleBook as XmlElement = xmldoc.CreateElement("book")
         dim attStyle as XmlAttribute = xmldoc.CreateAttribute("style")

         eleBook.SetAttributeNode(attStyle)
         eleBook.SetAttribute("style", "hardcover")

         dim root as XmlElement = xmldoc.Item("bookstore")
         root.AppendChild(eleBook)
         
         xmldoc.Save(Server.MapPath("Data.xml"))

         output.Text = "Append operation successful"

      catch ex as Exception
         output.Text = "Error accessing XML file"
      end try
     
   end sub
</script>

<html><body>
   <asp:Label id="output" runat="server" />
</body></html>


File: Data.xml

<?xml version="1.0"?>
<bookstore>
  <book genre="asdf">
    <title>asdf</title>
    <author>
      <first-name>asdf</first-name>
      <last-name>asdf</last-name>
    </author>
    <price>asdf</price>
  </book>
  <book genre="asdf">
    <title>asdf</title>
    <author>
      <first-name>asdf</first-name>
      <last-name>asdf</last-name>
    </author>
    <price>asdf</price>
  </book>
  <book genre="asdf">
    <title>asdf</title>
    <author>
      <first-name>asdf</first-name>
      <last-name>asdf</last-name>
    </author>
    <price>asdf</price>
  </book>
</bookstore>








25.10.XMLDocument
25.10.1.Append data to an XML file (VB.net)
25.10.2.Recursively load XML document with DOM