我在尝试序列化我的类时遇到错误。 xml模式由第三方定义,我无法更改它。具有相同名称但具有不同属性的序列化2属性
的XML看起来像这样(只片段的BTW):
<order>
<date type="received-date">20160810</date>
<date type="required-date">20160810</date>
<reference type="cust-order-no">CCCC</reference>
<reference type="an-2">AAAA</reference>
<reference type="an-3">BBBB</reference>
<order>
我已经定义了下面的类为我的订单日期
[Serializable]
public class OrderDate
{
public enum OrderDateTypeEnum
{
[Description("entered-date")]
EnteredDate,
[Description("received-date")]
ReceivedDate,
[Description("required-date")]
RequiredDate
}
private OrderDateTypeEnum _typeEnum;
[XmlIgnore]
public OrderDateTypeEnum TypeEnum
{
get { return this._typeEnum; }
set
{
this._typeEnum = value;
this.Type = this._typeEnum.GetDescription();
}
}
[XmlAttribute("type")]
public string Type { get; set; }
[XmlText]
public string Name { get; set; }
}
和秩序类被定义为:
[Serializable]
public class Order
{
[XmlElement("date")]
public OrderDate EnteredDate { get; set; }
[XmlElement("date")]
public OrderDate ReceivedDate { get; set; }
[XmlElement("date")]
public OrderDate RequiredDate { get; set; }
}
但是当我序列化这个,我得到一个错误:
The XML element 'date' from namespace '' is already present in the current
scope. Use XML attributes to specify another XML name or namespace for the
element.
当我删除[XmlElement("date")]
,它的工作原理,但我的XML没有格式化我需要的方式。
我会在参考时遇到与参考完全相同的问题。
我该如何绕过这个问题?
对于ReceivedDate属性,不应该是'[XmlElement(“received-date”)]''而不是'[XmlElement(“date”)]?所需日期也一样。我在你的XML中看不到“EnteredDate”。 – user3185569
@ user3185569,理想情况是,但是如果你按照你的建议做了,那么这个票据不会是而是它会是这在我的场景中并不好,因为记住我不能改变xml的格式,因为它来自第三方。 –
Thierry