c# - 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>
我尝试创建具有不同名称的元素并使用 设置新名称
[XmlElement("AddressLine")]
,但这不起作用。public string CompanyName { get; set; } public string AddressLine { get; set; } [XmlElement("AddressLine")] public string AddressLine1 { get; set; } [XmlElement("AddressLine")] public string AddressLine2 { get; set; } public string City { get; set; }
错误信息:
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.
如果我使用数组或列表不好或至少,我没有成功。
我的最后一个想法是将对象序列化为
AddressLine2
和AddressLine3
字符串,并将字符串中的重复值序列化为AddressLine
.
是在哪里创建对象的方法,在序列化后看起来像示例?有什么建议么?
感谢您的回答。
解决方案
推荐阅读
- netlogo - 首先基于属性然后随机创建到 n 个海龟的链接
- python - 如何并排显示 2 列 plt.imshow,Python
- struct - 如何在具有生命周期的结构上实现标记特征?
- r - 使用 R 中的 readLines 解析数据前的最后一个注释行
- typescript - 如何检查 Typescript Set 数据结构中的列表相等性?
- lua - '
'预计接近'结束' - android - 在某些三星 Android 设备上未找到处理意图 OPEN_DOCUMENT_TREE 的活动
- android - 找不到生命周期:common-java8 库
- javascript - 当我使用 react-native 时没有调用 passport.deserializeUser
- python - 组合两个数据框的行