VB.Net Tutorial/XML/XmlTextWriter

Материал из VB Эксперт
Версия от 12:54, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Build XmlDocument and Save it use the XmlTextWriter

Imports System.IO
Imports System.Xml
Imports System.Xml.Schema

public class Test
   public Shared Sub Main
        Dim Doc As New XmlDocument()
        Dim XmlProc As XmlDeclaration
        XmlProc = Doc.CreateXmlDeclaration("1.0", "UTF-8", "yes")
        Doc.AppendChild(XmlProc)
        " Create the root node.
        Dim Root As XmlElement
        Root = Doc.CreateElement("RootNode")
        " Create a child node.
        Dim Child As XmlElement
        Child = Doc.CreateElement("ChildNode")
        Child.InnerText = "Child Node Value"
        " Add an attribute to the child node.
        Dim Attr As XmlAttribute
        Attr = Doc.CreateAttribute("ChildAttribute")
        Attr.Value = "Child Node Attribute"
        Child.Attributes.Append(Attr)
        " Add a comment to the root node.
        Dim ChildComm As XmlComment
        ChildComm = Doc.CreateComment("This is a child node.")
        Root.AppendChild(ChildComm)
        " Place the child node within the root node.
        Root.AppendChild(Child)
        " Place the root node within the document.
        Doc.AppendChild(Root)
        " Write the document to disk.
        Dim Output As New XmlTextWriter("SimpleXml.xml", System.Text.Encoding.UTF8)
        Doc.WriteTo(Output)
        Output.Close()
   End Sub
End class

Create XML Document using XmlTextWriter

Imports System.Xml
Imports System.IO

public class Test
   public Shared Sub Main
        Dim memory_stream As New MemoryStream()
        Dim xml_text_writer As New XmlTextWriter(memory_stream, System.Text.Encoding.UTF8)
        xml_text_writer.Formatting = Formatting.Indented
        xml_text_writer.Indentation = 4
        xml_text_writer.WriteStartDocument(True)
        xml_text_writer.WriteStartElement("Employees")
        MakeEmployee(xml_text_writer, "A", "A", 11111)
        MakeEmployee(xml_text_writer, "B", "B", 22222)
        MakeEmployee(xml_text_writer, "C", "C", 33333)
        xml_text_writer.WriteEndElement()
        xml_text_writer.WriteEndDocument()
        xml_text_writer.Flush()
        Dim stream_reader As New StreamReader(memory_stream)
        memory_stream.Seek(0, SeekOrigin.Begin)
        Console.WriteLine(stream_reader.ReadToEnd())
        xml_text_writer.Close()
    End Sub
    Private Shared Sub MakeEmployee(ByVal xml_text_writer As XmlTextWriter, ByVal first_name As String, ByVal last_name As String, ByVal emp_id As Integer)
        xml_text_writer.WriteStartElement("Employee")
        xml_text_writer.WriteStartElement("FirstName")
        xml_text_writer.WriteString(first_name)
        xml_text_writer.WriteEndElement()
        xml_text_writer.WriteStartElement("LastName")
        xml_text_writer.WriteString(last_name)
        xml_text_writer.WriteEndElement()
        xml_text_writer.WriteStartElement("EmployeeId")
        xml_text_writer.WriteString(emp_id.ToString)
        xml_text_writer.WriteEndElement()
        xml_text_writer.WriteEndElement()
    End Sub
End class
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Employees>
    <Employee>
        <FirstName>A</FirstName>
        <LastName>A</LastName>
        <EmployeeId>11111</EmployeeId>
    </Employee>
    <Employee>
        <FirstName>B</FirstName>
        <LastName>B</LastName>
        <EmployeeId>22222</EmployeeId>
    </Employee>
    <Employee>
        <FirstName>C</FirstName>
        <LastName>C</LastName>
        <EmployeeId>33333</EmployeeId>
    </Employee>
</Employees>

Use XmlTextWriter to write xml document

Imports System.Xml.Serialization, System.IO
Imports System.Xml
Public Class Tester
    Public Shared Sub Main
    
      Dim myXmlTextWriter As New XmlTextWriter("doc.xml", Nothing)
      With myXmlTextWriter
         .Formatting = Formatting.Indented
         .Indentation = 3
         .IndentChar = " "
         .WriteStartDocument()
         .WriteComment("This is a sample XML document generated" & _
            " using an XMLTextWriter object.")
         .WriteStartElement("UserChoice")
         .WriteElementString("element", "value")
         .WriteEndElement()
         .Close()
      End With
    End Sub
End Class