简体   繁体   中英

C# XML deserialize all attributes as a collection

I have an XML file that looks like this:

<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

Cheers

Yes, you can use XmlAnyAttribute here, although personally I would suggest that the explicit member approach is far superior, ie your

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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