ASP.NET Tutorial/XML/XMLTextWriter — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:57, 26 мая 2010
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
Use XMLTextWriter to create XML file (VB.net)
<%@ 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>