ASP.NET Tutorial/XML/XMLTextWriter — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 18:30, 26 мая 2010

Use XmlTextWriter to create XML document, then use XmlTextReader to read it back

   <source lang="csharp">

<%@ 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">
       <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>
   
   </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 & "Type:" & xmlTR.NodeType.ToString & "
" If xmlTR.Name <> "" Then str = str & "Name:" & xmlTR.Name & "
" End If If xmlTR.Value <> "" Then str = str & "Value:" & xmlTR.Value & "
" End If If xmlTR.AttributeCount > 0 Then str = str & "Attribute:" Dim i As Integer For i = 0 To xmlTR.AttributeCount - 1 str = str & " " & xmlTR.GetAttribute(i) & " " Next str = str & "
" End If str = str & "
" End While Me.result.Text = str Else Me.result.Text = "Create XML document first" End If End Sub

End Class</source>


Use XMLTextWriter to create XML file (VB.net)

   <source lang="csharp">

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

  sub Page_Load(Sender as Object, e as EventArgs)
     dim writer as XMLTextWriter
        
     try
        writer = new XMLTextWriter(Server.MapPath("Data.xml"), nothing)
        
        writer.WriteStartDocument
        writer.Formatting = Formatting.Indented
        writer.Indentation = 3
        writer.WriteStartElement("bookstore")
        writer.WriteStartElement("book")
        writer.WriteAttributeString("genre", "history")
        writer.WriteElementString("title", "my title")
        writer.WriteElementString("price","6.99")
        writer.WriteStartElement("author")
        writer.WriteElementString("first-name","M")
        writer.WriteEndElement()
        writer.WriteEndElement()
        writer.WriteEndElement()
        writer.Flush
     catch ex as Exception
        Response.Write("Error accessing XML file")
     finally
        writer.Close
        Response.Write("Finished processing")
     end try
  end sub

</script> <html><body> </body></html></source>