首页 > 解决方案 > C# XmlSerializer 序列化具有相同名称的多个项目

问题描述

我需要创建可序列化 (XML) 对象,它必须具有三个相同的值AddressLine。这是 DHL 要求的,用于集成。从文档:

<xsd:sequence>
<xsd:element name="CompanyName" type="CompanyNameValidator" />
<xsd:element name="AddressLine" type="AddressLine" minOccurs="1" maxOccurs="3"/>
<xsd:element name="City" type="City" minOccurs="0"/>

我需要得到看起来像这样的 XML。

<Consignee>
    <CompanyName>Company</CompanyName>
    <AddressLine>address 1</AddressLine>
    <AddressLine>address 2</AddressLine>        
    <AddressLine>address 3</AddressLine>        
    <City>City</City>
</Consignee>

错误信息:

There was an error reflecting property 'AddressLine1'. ---> System.InvalidOperationException:
 The XML element 'AddressLine' from namespace '' is already present in the current scope. Use XML
 attributes to specify another XML name or namespace for the element.

是在哪里创建对象的方法,在序列化后看起来像示例?有什么建议么?

感谢您的回答。

标签: c#xmlxmlserializer

解决方案


推荐阅读