Results 1 to 1 of 1

Thread: Write XML using VB.NET

  1. #1

    Default Write XML using VB.NET

    This tutorial will show you how to write xml documents with VB.NET using the XmlTextWriter class from the System.Xml namespace. To view the article on how to read xml documents using VB.NET, click here

    Note: Remember to set the “write” permissions for your webserver on the directory where you like to write the XML document to.

    The XML classes in the System.Xml namespace that can be used to write XML documents are:

    • XmlWriter
    • XmlTextWriter


    XmlTextWriter is inherited from XmlWriter which means (in short) XmlWriter has a couple of extra features. But for the purpose of this tutorial and in most cases, you’ll only need to use XmlTextWriter.

    I’ve added comments to the lines of code below to explain what they do:

    Code:
    <%@ Page Language="VB" %>
    <%@ import Namespace="System.Xml" %>
    <script runat="server">
    Sub Page_Load(Src As Object, E As EventArgs)
    	
        '*** Written by user CWA, CoolWebAwards.com Forums. 18 March 2010
        '*** http://forum.coolwebawards.com/
    	
    	' *** XmlTextWriter
    	Dim myXmlWriter As New XmlTextWriter(server.MapPath("test.xml"), Nothing)
    	myXmlWriter.Formatting = Formatting.Indented
    	myXmlWriter.Indentation = 4
    
    	' *** Uncomment to use XmlWriter for extra settings if necessary. Comment the 3 lines above
    	'Dim myXmlSettings As New XmlWriterSettings
    	'myXmlSettings.Indent = True
    	'Dim myXmlWriter As XmlWriter = XmlWriter.Create(server.MapPath("test.xml"), myXmlSettings)
    	
    	myXmlWriter.WriteComment("http://forum.coolwebawards.com") ' <!--http://forum.coolwebawards.com-->
    	myXmlWriter.WriteStartElement("Products") ' <Products>
    	
    	Dim i As Integer
    	For i = 1 to 3
    		'http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter_members.aspx
    		myXmlWriter.WriteStartElement("Product") ' see next line
    		myXmlWriter.WriteAttributeString("Id", cstr(i)) ' <Product id="">
    		myXmlWriter.WriteElementString("price", "price element text for ID " & cstr(i)) ' <price>price element text for ID 1</price>
    		myXmlWriter.WriteElementString("weight", "weight element text for ID " & cstr(i)) ' <weight>price element text for ID 1</price>
    		myXmlWriter.WriteRaw(Environment.NewLine & "        <description>description element text for ID " & cstr(i) & "</description>" & Environment.NewLine) ' <description>description element text for ID 3</description> | Just an example of how to write raw data
    		myXmlWriter.WriteEndElement() '</Product>
    	Next
    	
    	myXmlWriter.WriteEndElement() ' </Products>
    	myXmlWriter.Close()
    	
    End Sub
    </script>
    The code above will produce the following xml document called test.xml:

    Code:
    <!--http://forum.coolwebawards.com-->
    <Products>
        <Product Id="1">
            <price>price element text for ID 1</price>
            <weight>weight element text for ID 1</weight>
            <description>description element text for ID 1</description>
    	</Product>
        <Product Id="2">
            <price>price element text for ID 2</price>
            <weight>weight element text for ID 2</weight>
            <description>description element text for ID 2</description>
    	</Product>
        <Product Id="3">
            <price>price element text for ID 3</price>
            <weight>weight element text for ID 3</weight>
            <description>description element text for ID 3</description>
    	</Product>
    </Products>
    Click here for more information on how to read an xml document using vb.net
    CWA - CoolWebAwards.com

  2. #2

    Default

    I am a new in this language but I am not understand the problem. The XMLDocument class has a Save method for writing the data out to a file.



    ________
    jasicajhon

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •