2017-10-13 131 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