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

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
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.NET Tutorial » XML » XMLTextWriter 
25. 16. 2. Use XmlTextWriter to create XML document, then use XmlTextReader to read it back
<%@ 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.