简体   繁体   English

C#XML将所有属性反序列化为集合

[英]C# XML deserialize all attributes as a collection

I have an XML file that looks like this: 我有一个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>

I'm currently deserialising like so: 我目前正在反序列化:

[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.
}

The issue is with Mapping nodes - I'd rather get a collection of it's attributes then create a string for each one - I'm not exactly sure how to get about this though - I've tried using XmlAnyAttribute and XmlArrayItem 问题在于Mapping节点 - 我宁愿得到它的属性集合,然后为每个节点创建一个字符串 - 我不确定如何解决这个问题 - 我尝试过使用XmlAnyAttribute和XmlArrayItem

Cheers 干杯

Yes, you can use XmlAnyAttribute here, although personally I would suggest that the explicit member approach is far superior, ie your 是的,你可以在这里使用XmlAnyAttribute ,虽然我个人建议明确的成员方法是优越的,即你的

public class XmlMapping
{
    [XmlAttribute] public string Category1 { get; set; }
    [XmlAttribute] public string Category2 { get; set; }
    [XmlAttribute] public string Category3 { get; set; }
    [XmlAttribute] public string Category4 { get; set; }
    [XmlAttribute] public string Category5 { get; set; }
    [XmlAttribute] public string Category6 { get; set; }
    [XmlAttribute] public string Category7 { get; set; }
}

However, this works too: 但是,这也有效:

public class XmlMapping
{
    [XmlAnyAttribute]
    public XmlAttribute[] Attributes { get; set; }
}

then just enumerate it: 然后只列举它:

foreach (var map in subCat.Mappings)
{
    var attribs = map.Attributes;
    if (attribs != null)
    {
        foreach (var attrib in attribs)
        {
            System.Console.WriteLine("{0}={1}", attrib.Name, attrib.Value);
        }
    }
}

Works fine for me... (although I had to fix your broken xml - missing an end-tag) 对我来说工作正常...(虽然我必须修复你破碎的xml - 缺少一个结束标记)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM