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

Home
ASP.NET Tutorial
1.ASP.Net Instroduction
2.Language Basics
3.ASP.net Controls
4.HTML Controls
5.Page Lifecycle
6.Response
7.Collections
8.Validation
9.Development
10.File Directory
11.Sessions
12.Cookie
13.Cache
14.Custom Controls
15.Profile
16.Configuration
17.LINQ
18.ADO.net Database
19.Data Binding
20.Ajax
21.Authentication Authorization
22.I18N
23.Mobile
24.WebPart
25.XML
ASP.Net
Silverlight
ASP.NET Open Source
ASP.NET Tutorial » XML » XMLDocument 
25.10.1.Append data to an XML file (VB.net)
<%@ 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
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.