简体   繁体   中英

Deserializing XML Based on Element Attributes

I am trying to deserialize an XML file within a C# program that looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<Addresses>
  <ListName>Flowers</ListName>
  <Address contextRef="RP.CC">Some Address</Address>
  <Address contextRef="RP.BE">Some Other Address</Address>
  <Address contextRef="RP.BV">Yet Another Address</Address>
  <Address contextRef="RP.CAL">Wow, I Can't Believe It's Another Address</Address>
</Addresses>

I do not have any control over the format of this file. But, it will always have some combination of these 4 Address elements (ie these 4 contextRef attribute values are the only ones used) with differing element values each time.

Now, instead of deserializing into an Address array, I need to send them to individual properties within an Addresses object. My Current implementation uses an array and then a setter method to set these properties based on the contextRef as so:

public class Addresses
{
    [XmlElement("ListName")]
    public string ListName { get; set; }

    private Address[] _addresses;

    [XmlElement("Address")]
    public Address[] AddressesArray
    {
        get
        {
            return _addresses;
        }
        set
        {
            _addresses = value;
            SetAddress();
        }
    }

    [XmlIgnore]
    public Address AddressG21 { get; set; }

    [XmlIgnore]
    public Address AddressG22 { get; set; }

    [XmlIgnore]
    public Address AddressG23 { get; set; }

    [XmlIgnore]
    public Address AddressG9 { get; set; }

    private void SetAddress()
    {
        foreach (var address in _addresses)
        {
            if (address.ContextRef == "RP.CC")
            {
                AddressG21 = address;
            }
            else if (address.ContextRef == "RP.BE")
            {
                AddressG22 = address;
            }
            else if (address.ContextRef == "RP.BV")
            {
                AddressG23 = address;
            }
            else if (address.ContextRef == "RP.CAL")
            {
                AddressG9 = address;
            }
        }
    }
}

Where the Address object is defined as so:

public class Address
{
    private string valueField;

    /// <remarks/>
    [XmlText]
    public string Value
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }

    [XmlAttribute("contextRef")]
    public string ContextRef { get; set; }
}

So, my question is, is there a neater/better way of deserializing this XML directly into the AddressG21, etc. object properties without first using the Address array?

Thanks in advance.

I would use xml linq and create a dictionary in the class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Addresses addresses = doc.Descendants("Addresses").Select(x => new Addresses() {
                ListName = (string)x.Element("ListName"),
                dict = x.Elements("Address")
                   .GroupBy(y => (string)y.Attribute("contextRef"), z => (string)z)
                   .ToDictionary(y => y.Key, z => z.FirstOrDefault())
            }).FirstOrDefault();

        }
    }
    public class Addresses
    {
        public string ListName { get; set; }
        public Dictionary<string, string> dict { get; set; }
    }

}

If you had multiple Addresses elements then use this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<Addresses> addresses = doc.Descendants("Addresses").Select(x => new Addresses() {
                ListName = (string)x.Element("ListName"),
                dict = x.Elements("Address")
                   .GroupBy(y => (string)y.Attribute("contextRef"), z => (string)z)
                   .ToDictionary(y => y.Key, z => z.FirstOrDefault())
            }).ToList();

        }
    }
    public class Addresses
    {
        public string ListName { get; set; }
        public Dictionary<string, string> dict { get; set; }
    }

}

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