⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xml-via-textwriter.aspx

📁 This is a book about vb.you could learn this from this book
💻 ASPX
字号:
<%@Page Language="VB" %>
<%@Import Namespace="System.XML" %>
<%@ Import Namespace="System.IO" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Creating an XML document with an XMLTextWriter object</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Creating an XML document with an XMLTextWriter object</span><hr />
<!--------------------------------------------------------------------------->

<div id="outDocURL" runat="server"></div>
<div id="outError" runat="server">&nbsp;</div>
<div id="outResults" runat="server"></div>

<script language="vb" runat="server">

Sub Page_Load()

   'create physical path for the new file (in same folder as ASPX page)
   Dim strCurrentPath As String = Request.PhysicalPath
   Dim strXMLPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "\")) & "newbooklist.xml"

   'declare a variable to hold an XmlTextWriter object
   Dim objXMLWriter As XmlTextWriter

   Try

      'create a new objXMLWriter object for the XML file
      objXMLWriter = New XmlTextWriter(strXMLPath, Nothing)
      outDocURL.innerHTML = "Writing to file: <b>" & strXMLPath & "</b>"


   Catch objError As Exception

      'display error details
      outError.innerHTML = "<b>* Error while accessing document</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution

   End Try

   'now ready to write (or "push") the nodes for the new XML document

   'turn on indented formatting and set indent to 3 chararcters
   objXMLWriter.Formatting = Formatting.Indented
   objXMLWriter.Indentation = 3

   'start the document with the XML declaration tag
   objXMLWriter.WriteStartDocument()

   'write a comment element including the current date/time
   objXMLWriter.WriteComment("Created using an XMLTextWriter - " & Now())

   'write the opening tag for the <BookList> root element
   objXMLWriter.WriteStartElement("BookList")

      'write the opening tag for a <Book> element
      objXMLWriter.WriteStartElement("Book")

         'add two attributes to this element's opening tag
         objXMLWriter.WriteAttributeString("Category", "Technology")
         Dim intPageCount As Integer = 1248   'numeric value to convert
         objXMLWriter.WriteAttributeString("Pagecount", intPageCount.ToString("G"))

         'write four elements, using different source data types
         objXMLWriter.WriteElementString("Title", "Professional Video Recorder Programming")
         Dim datReleaseDate As DateTime = #03/03/2000#
         objXMLWriter.WriteElementString("ReleaseDate", datReleaseDate.ToString("yyyy-MM-dd"))
         Dim intSales As Integer = 17492
         objXMLWriter.WriteElementString("Sales", intSales.ToString("G"))
         Dim blnHardback As Boolean = True
         objXMLWriter.WriteElementString("Hardback", blnHardback.ToString())

         'write the opening tag for the <AuthorList> child element
         objXMLWriter.WriteStartElement("AuthorList")

            'add two <Author> elements
            objXMLWriter.WriteElementString("Author", "Francesca Unix")
            objXMLWriter.WriteElementString("Author", "William Soft")

         'close the <AuthorList> element
         objXMLWriter.WriteEndElement()

      'close the <Book> element
      objXMLWriter.WriteEndElement()

   'close the root <BookList> element
   objXMLWriter.WriteEndElement()

   'flush the current content to the file and close it
   objXMLWriter.Flush()
   objXMLWriter.Close()

   'now open the new XML file and read it into a string
   Dim strXMLResult As String
   Dim objSR As StreamReader = File.OpenText(strXMLPath)
   strXMLResult = objSR.ReadToEnd()
   objSR.Close
   objSR = Nothing

   'and display the results in the page
   outResults.innerHTML = "<pre>" & Server.HtmlEncode(strXMLResult) & "<pre>"

End Sub

</script>

<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -