1

我有一个在提交数据时必须匹配的 XML 文件。无论我做什么,我最终都会为主数组添加一个额外的包装器。XML 必须如下所示:

<?xml version="1.0" encoding="utf-8"?>
<contracts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <contract>
        <productType>VSC</productType>
        <contractNumber>ABS1869169</contractNumber>
        <vehicle>
            <vin>JTKDE177160100198</vin>
            <odometer>128135</odometer>
            <purchasePrice>5574</purchasePrice>
      </vehicle>
    </contract>
    <contract>
        <productType>VSC</productType>
        <contractNumber>ABS1865421</contractNumber>
        <vehicle>
            <vin>LTGSE177160154878</vin>
            <odometer>119154</odometer>
            <purchasePrice>6509</purchasePrice>
        </vehicle>
    </contract>
</contracts>

我最终得到了这样的额外合同:

<?xml version="1.0" encoding="utf-8"?>
<contracts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <contracts>
    <contract>
      <productType>VSC</productType>
      <contractNumber>ABS1869169</contractNumber>
      <vehicle>
        <vin>JTKDE177160100198</vin>
        <odometer>128135</odometer>
        <purchasePrice>5574</purchasePrice>
      </vehicle>
    </contract>
    <contract>
      <productType>VSC</productType>
      <contractNumber>ABS1865421</contractNumber>
      <vehicle>
        <vin>LTGSE177160154878</vin>
        <odometer>119154</odometer>
        <purchasePrice>6509</purchasePrice>
      </vehicle>
    </contract>
  </contracts>
</contracts>

这是我的代码。

Public Class contracts

    Public contracts() As contract

End Class


Public Class contract
    Public productType As String = "VSC"
    Public contractNumber As String
    Public vehicle As New contractVehicle
End Class

Public Class contractVehicle
    Public vin As String
    Public odometer As String
    Public purchasePrice As String
End Class


    Dim serializer As New XmlSerializer(GetType(contracts))
    Using stream As Stream = New FileStream("C:\Code\TestXML2.xml", FileMode.Create)
        Dim s As New XmlWriterSettings()
        s.Indent = True
        s.Encoding = Encoding.UTF8
        Dim writer As XmlWriter = XmlTextWriter.Create(stream, s)
        serializer.Serialize(writer, vx)
        writer.Close()
    End Using
4

1 回答 1

2

我终于找到了自己问题的答案。我只需要设置属性。

Public Class contracts

    <XmlElementAttribute("contract", Form:=Xml.Schema.XmlSchemaForm.Unqualified)> _
    Public contracts() As contract

    End Sub

End Class
于 2013-10-16T23:53:29.147 回答