我有一个如下所示的 XML 文件:
<Categories>
<Category Name="Mobile">
<SubCategory Name="Mobile Phones" MarkUp="1.2">
<Mapping Category1="COMMUNICATION" Category2="PHONE" Category3="MOBILE" Category4="MOBILE PHONE" Category5="ANDROID" Category6="" Category7="" />
<Mapping Category1="COMMUNICATION" Category2="PHONE" Category3="MOBILE" Category4="MOBILE PHONE" Category5="BLACKBERRY OS" Category6="" Category7="" />
<Mapping Category1="COMMUNICATION" Category2="PHONE" Category3="MOBILE" Category4="MOBILE PHONE" Category5="OTHER" Category6="" Category7="" />
<Mapping Category1="COMMUNICATION" Category2="PHONE" Category3="MOBILE" Category4="MOBILE PHONE" Category5="SYMBIAN" Category6="" Category7="" />
<Mapping Category1="COMMUNICATION" Category2="PHONE" Category3="MOBILE" Category4="MOBILE PHONE" Category5="WINDOWS PHONE" Category6="" Category7="" />
</SubCategory>
</Categories>
我目前正在像这样反序列化:
[XmlRoot(ElementName = "Category")]
public class XmlCategory
{
[XmlAttribute(AttributeName = "Name")]
public string Name { get; set; }
[XmlElement("SubCategory")]
public XmlSubCategory[] XmlSubCategories { get; set; }
}
public class XmlSubCategory
{
[XmlAttribute(AttributeName = "Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "MarkUp")]
public string MarkUp { get; set; }
[XmlElement("Mapping")]
public XmlMapping[] Mappings { get; set; }
}
public class XmlMapping
{
[XmlAttribute(AttributeName = "Category1")]
public string Category1 { get; set; }
// etc.
}
问题在于映射节点 - 我宁愿获取它的属性集合,然后为每个属性创建一个字符串 - 我不完全确定如何解决这个问题 - 我尝试使用 XmlAnyAttribute 和 XmlArrayItem
干杯