【发布时间】:2014-04-30 09:26:41
【问题描述】:
我正在从数据库表列中检索并成功反序列化已知元素名称的 xml 字符串(这些名称不会更改),但也有一些称为“其他属性”的嵌套 XML 元素并不总是已知的。我在将这些未知元素名称反序列化为通用列表时遇到了一些麻烦,因此我可以在反序列化后将它们显示在 html 中。
XML如下:
<Detail>
<DetailAttributes>
<Name>Name_123</Name>
<Type>Type_123</Type>
</DetailAttributes>
<OtherAttributes>
<SummaryKey AttributeName="SummaryKey">SummaryKey_123</SummaryKey>
<Account AttributeName="Account">Account_123</Account>
</OtherAttributes>
</Detail>
反序列化“名称”和“类型”元素没有问题,我可以反序列化“SummaryKey”和“帐户”元素,但前提是我明确指定它们的元素名称 - 这不是所需的方法,因为“OtherAttributes” ' 可能会发生变化。
我的课程如下:
[XmlRoot("Detail")]
public class objectDetailsList
{
[XmlElement("DetailAttributes"), Type = typeof(DetailAttribute))]
public DetailAttribute[] detailAttributes { get; set; }
[XmlElement("OtherAttributes")]
public List<OtherAttribute> otherAttributes { get; set; }
public objectDetailsList()
{
}
}
[Serializable]
public class Detail Attribute
{
[XmlElement("Type")]
public string Type { get;set; }
[XmlElement("Name")]
public string Name { get;set; }
public DetailAttribute()
{
}
}
[Serializable]
public class OtherAttribute
{
//The following will deserialise ok
//[XmlElement("SummaryKey")]
//public string sumKey { get; set; }
//[XmlElement("Account")]
//public string acc { get; set; }
//What I want to do below is create a list of all 'other attributes' without known names
[XmlArray("OtherAttributes")]
public List<Element> element { get; set; }
}
[XmlRoot("OtherAttributes")]
public class Element
{
[XmlAttribute("AttributeName")]
public string aName { get; set; }
[XmlText]
public string aValue { get; set; }
}
当我尝试检索 OtherAttribute 元素的反序列化列表时,计数为零,因此它无法访问嵌套在“其他属性”中的元素。
有人可以帮我解决这个问题吗?
【问题讨论】: