XML-生成具有相同名称问题的辅助元素

时间:2019-07-17 07:51:06

标签: xml vb.net

我无法在配置元素下的同一级别中创建多个具有相同名称的VB.NET XML元素。我只能用attribute的“ Description1”创建一个name。我到底不明白什么?

  <configuration name="Default" quantity="1">
    <attribute name="Description1" value="1" />
    <attribute name="Description2" value="1" />
  </configuration>

我试图将属性元素更改为数组,但我想我不知道自己在做什么。

Public Class transaction
    <Xml.Serialization.XmlElement>
    Public Property document() As document

    <Xml.Serialization.XmlAttribute>
    Public Property type() As String
    <Xml.Serialization.XmlAttribute>
    Public Property [date]() As Integer
    <Xml.Serialization.XmlAttribute>
    Public Property name() As String

End Class

Public Class document
    <Xml.Serialization.XmlAttribute>
    Public Property aliasset As String
    <Xml.Serialization.XmlAttribute>
    Public Property eid As String

    <Xml.Serialization.XmlElement>
    Public Property configuration As Configuration
End Class

Public Class configuration
    <Xml.Serialization.XmlAttribute>
    Public Property name As String
    <Xml.Serialization.XmlAttribute>
    Public Property quantity As Integer
    <Xml.Serialization.XmlElement>
    Public Property attribute As attribute
End Class

Public Class attribute
    <Xml.Serialization.XmlAttribute>
    Public Property name As String
    <Xml.Serialization.XmlAttribute>
    Public Property value As String
End Class

我正在尝试与此配合使用:

Private Sub Create_button_Click(sender As Object, e As EventArgs) Handles Create_button.Click

    Dim cf As New Transactions()
    cf.transaction = New transaction() With {.type = "wf_export_document_attributes", .date = 1235456, .name = "QUALITY"}
    cf.transaction.document = New document() With {.aliasset = "", .eid = "1234567"}
    cf.transaction.document.configuration = New configuration() With {.name = "Default", .quantity = "1"}
    cf.transaction.document.configuration.attribute = New attribute() With {.name = "Description", .value = "1"}
    cf.transaction.document.configuration.attribute = New attribute() With {.name = "Description2", .value = "1"}

    Dim s As New System.Xml.Serialization.XmlSerializer(GetType(Transactions))

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

1 个答案:

答案 0 :(得分:0)

您需要某种类型的变量,它可以容纳多个attribute,例如List(Of Attribute),如下所示:

Public Class configuration
    <Xml.Serialization.XmlAttribute>
    Public Property name As String

    <Xml.Serialization.XmlAttribute>
    Public Property quantity As Integer

    <Xml.Serialization.XmlElement(ElementName:="attribute")>
    Public Property attributes As List(Of attribute)

End Class

然后添加它们:

Dim cf As New Transactions()
cf.transaction = New transaction() With {.type = "wf_export_document_attributes", .date = 1235456, .name = "QUALITY"}
cf.transaction.document = New document() With {.aliasset = "", .eid = "1234567"}
cf.transaction.document.configuration = New configuration() With {.name = "Default", .quantity = 1}
cf.transaction.document.configuration.attributes = New List(Of attribute) From {New attribute With {.name = "Description1", .value = "1"}}
cf.transaction.document.configuration.attributes.Add(New attribute() With {.name = "Description1", .value = "1"})

Dim s As New System.Xml.Serialization.XmlSerializer(GetType(Transactions))

Using fs As New System.IO.FileStream("C:\temp\file.xml", System.IO.FileMode.Create)
    s.Serialize(fs, cf)
End Using

是什么原因造成的:

<?xml version="1.0"?>
<Transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <transaction type="wf_export_document_attributes" date="1235456" name="QUALITY">
    <document aliasset="" eid="1234567">
      <configuration name="Default" quantity="1">
        <attribute name="Description1" value="1" />
        <attribute name="Description1" value="1" />
      </configuration>
    </document>
  </transaction>
</Transactions>

N.B。当我指定ElementName:="attribute"时,它在末尾添加了一个额外的“ s>”。我不知道为什么如果它对您造成了同样的问题,您可以仅将该属性命名为“ attribute”而不是“ attributes”。愚蠢的我,我没有发现FileMode.OpenOrCreate搞砸了。