Use XmlTextWriter to create XML document, then use XmlTextReader to read it back : XMLTextWriter « XML « ASP.NET Tutorial






<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="MyPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Create XML Document" />
        <asp:Button ID="Button2" runat="server" Text="Display XML Document" />
        <asp:Label ID="result" runat="server Text="Label"></asp:Label>
    
    </div>
    </form>
</body>
</html>

File: Default.aspx.vb

Imports System.Xml
Imports System.IO

Partial Class MyPage
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.result.Text = ""

        Dim xmlTW As New XmlTextWriter("Data.xml", Nothing)

        xmlTW.WriteStartDocument()

        xmlTW.WriteStartElement("A")

        xmlTW.WriteStartElement("B")
        xmlTW.WriteAttributeString("C", "D", "E")

        xmlTW.WriteStartElement("F")
        xmlTW.WriteString("97")
        xmlTW.WriteEndElement()

        xmlTW.WriteEndElement()

        xmlTW.WriteStartElement("G")
        xmlTW.WriteAttributeString("H", "I", "J")

        xmlTW.WriteStartElement("K")
        xmlTW.WriteString("99")
        xmlTW.WriteEndElement()

        xmlTW.WriteStartElement("L")
        xmlTW.WriteString("95")
        xmlTW.WriteEndElement()

        xmlTW.WriteEndElement()

        xmlTW.WriteEndElement()

        xmlTW.WriteEndDocument()

        xmlTW.Close()

        Me.result.Text = "XML document created"

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Me.result.Text <> "" Then
            Dim fs As New FileStream("Data.xml", FileMode.Open)
            Dim xmlTR As New XmlTextReader(fs)
            Dim str As String
            While xmlTR.Read()
                str = str & "<b>Type:</b>" & xmlTR.NodeType.ToString & "<br>"

                If xmlTR.Name <> "" Then
                    str = str & "<b>Name:</b>" & xmlTR.Name & "<br>"
                End If

                If xmlTR.Value <> "" Then
                    str = str & "<b>Value:</b>" & xmlTR.Value & "<br>"
                End If

                If xmlTR.AttributeCount > 0 Then
                    str = str & "<b>Attribute:</b>"
                    Dim i As Integer
                    For i = 0 To xmlTR.AttributeCount - 1
                        str = str & " " & xmlTR.GetAttribute(i) & " "
                    Next
                    str = str & "<br>"
                End If
                str = str & "<br>"
            End While
            Me.result.Text = str
        Else
            Me.result.Text = "Create XML document first"
        End If

    End Sub
End Class








25.16.XMLTextWriter
25.16.1.Use XMLTextWriter to create XML file (VB.net)
25.16.2.Use XmlTextWriter to create XML document, then use XmlTextReader to read it back