2017-10-13 162 views
0

我想用VB.net創建XML文件

在Visual Studio中從頭開始創建XML文件,我在網上找到的例子,但我努力去理解如何在某些情況下,添加屬性和其他情況下的子元素。再加上我的頂級元素有其他elements.I意味着我的格式是mucher長,但它看起來像下面的例子:

-<CompanyFile> 
     -<Companybranch name="something"> 
     -<Customer> 
      <name></name> 
      <age></age> 
      <address> 
       <addreesLine > </addreesLine> 
      <address> 
      </Customer> 
     </Companybranch> 
     </CompanyFile> 

我發現,有基本的XML格式的鏈接。

Imports System.Xml 

     Public Class Form1 
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim writer As New XmlTextWriter("product.xml", System.Text.Encoding.UTF8) 
    writer.WriteStartDocument(True) 
    writer.Formatting = Formatting.Indented 
    writer.Indentation = 2 
    writer.WriteStartElement("Table") 
    createNode(1, "Product 1", "1000", writer) 
    createNode(2, "Product 2", "2000", writer) 
    createNode(3, "Product 3", "3000", writer) 
    createNode(4, "Product 4", "4000", writer) 
    writer.WriteEndElement() 
    writer.WriteEndDocument() 
    writer.Close() 
End Sub 
Private Sub createNode(ByVal pID As String, ByVal pName As String, ByVal pPrice As String, ByVal writer As XmlTextWriter) 
    writer.WriteStartElement("Product") 
    writer.WriteStartElement("Product_id") 
    writer.WriteString(pID) 
    writer.WriteEndElement() 
    writer.WriteStartElement("Product_name") 
    writer.WriteString(pName) 
    writer.WriteEndElement() 
    writer.WriteStartElement("Product_price") 
    writer.WriteString(pPrice) 
    writer.WriteEndElement() 
    writer.WriteEndElement() 
End Sub 

末級

我怎麼能創造我想要的一個節點元素的列表,然後元素的列表可能會或可能不會有孩子或屬性?

謝謝!

+1

您發佈的代碼有效並且非常直接。你有什麼嘗試? – Neal

+0

它不是我害怕。根有三個元素,兩個有屬性,而第三個沒有。然後這些元素具有子元素等。該代碼不解釋如何工作 – Warda

+0

創建類來表示您的數據然後使用'XmlSerializer'轉換爲XML會更容易。 – Crowcoder

回答

2

您可以使用的功能,只有vb.net語言有 - XML Literalsembed expressions

Dim companyBranchName As String = "My Company" 
Dim customerName As String = "Some customer Ltd" 
Dim customerAge As Integer = 33 
Dim addressLine As String = "Line 12" 

Dim document As XDocument = <?xml version="1.0" encoding="UTF-8"?> 
          <CompanyFile> 
           <Companybranch name=<%= companyBranchName %>> 
            <Customer> 
             <name><%= customerName %></name> 
             <age><%= customerAge %></age> 
             <address> 
              <addreesLine><%= addressLine %></addreesLine> 
             <address> 
            </Customer> 
           </Companybranch> 
          </CompanyFile> 

document.Save("path-to-the-file.xml) 

另一種方法是使用序列化。 XmlSerializer Class
創建代表你的數據結構類,如其他答案

Dim xmlDeclaration As New XDeclaration("1.0", "UTF-8", "yes") 
Dim document As XDocument = _ 
    New XDocument(xmlDeclaration, 
        new XElement("CompanyFile", 
           new XElement("CompanyBranch", 
              new XAttrbute("name", companyName), 
              new XElement("Customer", "...")))); 

document.Save("path-to-the-file.xml) 

提到如果

Public Class CompanyFile 
    Public Property CompanyBranch As CompanyBranch 
End Class 

Public Class CompanyBranch 
    <XmlAttribute("name")> ' use attributes for explicitly declaring serialization logic 
    Public Property Name As String 
    Public Property Customer As Customer 
End Class 

Public Class Customer 
    Public Property Name As String 
    Public Property Age As Integer 
    Public Property Address As Address 
End Class 

Public Class Address 
    Public Property AddressLine As String 
End Class 

然後序列數據文件

Dim data = New CompanyFile With 
{ 
    .CompanyBranch = New CompanyBranch With 
    { 
     .Name = "something", 
     .Customer = New Customer With 
     { 
      .Name = "customer name", 
      .Age = 33, 
      .Address = New Address With 
      { 
       .AddressLine = "Line 33" 
      } 
     } 
    } 
} 

Dim serializer = New XmlSerializer(GetType(CompanyFile)) 
Using writer As New StreamWriter("path-to-file.xml") 
    serializer.Serialize(writer, data) 
End Using 

或SE LINQ到XML你要保存非常大量的數據,那麼你可以使用你的原始方法XmlWriter,它不那麼重要易於維護和更難維護,但對於大量數據非常有效,使用XmlWriter您不需要將全部數據存儲在內存中 - 您可以將數據寫入更小的塊中。

Dim settings As New XmlWriterSettings With 
{ 
    settings.Indent = True 
}  
Using writer As XmlWriter = XmlWriter.Create("path-to-file.xml", settings) 
    writer.WriteStartElement("CompanyFile") 
    ' other elements 
    writer.WriteEndElement() 
End Using 
+0

非常感謝你! :) – Warda

0

我會使用Xml Linq這是一個更新的網絡庫,具有更好的功能,然後是舊的Xml網絡庫。

見下面的代碼:

Imports System.Xml 
Imports System.Xml.Linq 
Module Module1 

    Sub Main() 
     Dim companyName As String = "Acme" 
     Dim customerName As String = "John" 
     Dim age As Integer = 2525 
     Dim address As String = "123 Main St." 

     Dim companyFile As XElement = New XElement("CompanyFile", 
      New XElement("Companybranch", New Object() { 
         New XAttribute("name", companyName), 
         New XElement("Customer", New Object() { _ 
             New XElement("name", customerName), 
             New XElement("age", age), 
             New XElement("address", 
                New XElement("addressLine", address)) 
            }) 
        }) 
      ) 

    End Sub 

End Module 
+0

謝謝加載! :) – Warda

0

使用XML序列化

有了這些類,

<Xml.Serialization.XmlRoot> 
Public Class CompanyFile 
    <Xml.Serialization.XmlElement> 
    Public Property Companybranch() As Companybranch 
End Class 

Public Class Companybranch 
    <Xml.Serialization.XmlElement> 
    Public Property Customer() As Customer 
    <Xml.Serialization.XmlAttribute> 
    Public Property name() As String 
End Class 

Public Class Customer 
    <Xml.Serialization.XmlElement> 
    Public Property name As String 
    <Xml.Serialization.XmlElement> 
    Public Property age As Integer 
    <Xml.Serialization.XmlElement> 
    Public Property address As address 
End Class 

Public Class address 
    <Xml.Serialization.XmlElement> 
    Public Property addreesLine As String 
End Class 

和驗證碼,

Dim cf As New CompanyFile() 
cf.Companybranch = New Companybranch() With {.name = "company name"} 
cf.Companybranch.Customer = New Customer() With {.name = "person name", .age = 4} 
cf.Companybranch.Customer.address = New address() With {.addreesLine = "123 abc st."} 

Dim s As New Xml.Serialization.XmlSerializer(GetType(CompanyFile)) 

Using fs As New System.IO.FileStream("file.xml", System.IO.FileMode.OpenOrCreate) 
    s.Serialize(fs, cf) 
End Using 

,你可以這樣寫xml文件,

<?xml version="1.0"?> 
<CompanyFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Companybranch name="company name"> 
    <Customer> 
     <name>person name</name> 
     <age>4</age> 
     <address> 
     <addreesLine>123 abc st.</addreesLine> 
     </address> 
    </Customer> 
    </Companybranch> 
</CompanyFile> 

所有使用強類型的對象。

+0

非常感謝你! :) – Warda