我想使用 VB.net 在 Visual Studio 中从头开始创建 XML 文件
我找到了在线示例,但我很难理解如何在某些情况下添加属性和其他情况下的子元素。加上我的顶部元素有其他元素。我的意思是我的格式更长,但它看起来像下面的例子:
-<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
结束类
我怎样才能创建我想要的东西 一个节点有元素列表,然后元素列表可能有也可能没有 child 或属性。?
谢谢你!
请您参考如下方法:
您可以使用只有 vb.net 语言具有的功能 - XML Literals 和 embed 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
创建代表您的数据结构的类
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
或者像其他答案中提到的那样使用 LINQ to Xml
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)
如果您要保存大量数据,那么您可以将原始方法与
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