VB.Net/XML/XML File Creation — различия между версиями

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

Текущая версия на 12:42, 26 мая 2010

Build an XML Document

 
Imports System
Imports System.Xml
Imports System.IO
Public Class MainClass
   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", 1)
        MakeEmployee(xml_text_writer, "B", "B", 2)
        MakeEmployee(xml_text_writer, "C", "C", 3)
        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 
    " Add a node to the document.
   Shared Private Sub MakeEmployee(ByVal xml_text_writer As XmlTextWriter, ByVal first_name As String, ByVal last_name As String, ByVal emp_id As Integer)
        " Start the Employee element.
        xml_text_writer.WriteStartElement("Employee")
        " Write the FirstName.
        xml_text_writer.WriteStartElement("FirstName")
        xml_text_writer.WriteString(first_name)
        xml_text_writer.WriteEndElement()
        " Write the LastName.
        xml_text_writer.WriteStartElement("LastName")
        xml_text_writer.WriteString(last_name)
        xml_text_writer.WriteEndElement()
        " Write the EmployeeId.
        xml_text_writer.WriteStartElement("EmployeeId")
        xml_text_writer.WriteString(emp_id.ToString)
        xml_text_writer.WriteEndElement()
        " Close the Employee element.
        xml_text_writer.WriteEndElement()
    End Sub
End Class


Create XML document: XmlDocument, XmlAttribute, XmlElement

 
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections
Imports System.Data
Imports System.Windows.Forms

Public Class MainClass
    
    Shared Sub Main()
        Dim FakeQuantity As Integer
        Dim Doc As New XmlDocument()
        Dim newAtt As XmlAttribute
        Dim TempNode As XmlElement
        " Use the XmlDeclaration class to place the
        " <?xml version="1.0"?> declaration at the top of our XML file
        Dim dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", _
                                         Nothing, Nothing)
        Doc.AppendChild(dec)
        Dim DocRoot As XmlElement = Doc.CreateElement("Orders")
        Doc.AppendChild(DocRoot)
        " Generate a couple of phony orders
        Dim x As Integer
        For x = 0 To 11
            Dim Order As XmlNode = Doc.CreateElement("Order")
            newAtt = Doc.CreateAttribute("Quantity")
            FakeQuantity = 10 * x + x
            newAtt.Value = FakeQuantity.ToString()
            Order.Attributes.Append(newAtt)
            DocRoot.AppendChild(Order)
        Next
        Doc.Save("OutDocument.xml")
    End Sub
End Class


Creating Xml with XmlTextWriter

  
Imports System.IO
Imports System.Xml
Public Class Form1
    Public Shared Sub Main()
        Dim fs As FileStream = New FileStream("aaa", FileMode.Create)
        Dim w As XmlTextWriter = New XmlTextWriter(fs, Nothing)
        w.WriteStartDocument()
        w.WriteStartElement("mydocument")
        w.WriteAttributeString("name", "", "Description")
        w.WriteComment("This is the comment, etc.")
        w.WriteStartElement("person")
        w.WriteStartElement("name")
        w.WriteString("M")
        w.WriteEndElement()
        w.WriteStartElement("phone")
        w.WriteString("555-5555")
        w.WriteEndElement()
        w.WriteEndElement()
        w.WriteEndElement()
        w.WriteEndDocument()
        w.Close()
    End Sub
End Class


Output with XmlWriter

  
Imports System.Xml
Public Class Example5
    Shared Sub Main
        Dim writer As XmlWriter
        writer = XmlWriter.Create("output.xml")
        writer.WriteStartDocument()
        writer.WriteStartElement("pubs")
        writer.WriteStartElement("titles")
        writer.WriteStartAttribute("name")
        writer.WriteValue("Database Guide")
        writer.WriteEndAttribute()
        writer.WriteStartAttribute("price")
        writer.WriteValue(19.99)
        writer.WriteEndAttribute()
        writer.WriteEndElement()
        writer.WriteEndElement()
        writer.Close()
    End Sub
End Class


Read and write XML with XmlReader and XmlWriter

  
Imports System.Xml
Public Class Example1
    Shared Sub Main()
        Dim reader As XmlReader
        Dim writer As XmlWriter
        reader = XmlReader.Create("pubs.xml")
        writer = XmlWriter.Create("output.xml")
        While reader.Read()
            writer.WriteNode(reader, True)
        End While
        reader.Close()
        writer.Close()
    End Sub
End Class


Use XmlWriter to generate XML document

 
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO

Public Class MainClass
   Public Shared Sub Main()
        Dim myXmlSettings As New XmlWriterSettings
        myXmlSettings.Indent = True
        myXmlSettings.NewLineOnAttributes = True
        Using ProductWriter As XmlWriter = XmlWriter.Create("test.xml", myXmlSettings)
            ProductWriter.WriteComment("www.vbex.ru ")
            ProductWriter.WriteStartElement("Product")
            ProductWriter.WriteAttributeString("Id", "101")
            ProductWriter.WriteAttributeString("COunt", "10")
            ProductWriter.WriteElementString("P1", "P2")
            ProductWriter.WriteEndElement() 
        End Using
   End Sub
End Class