0

I am trying to deserialize the custom config keys using a common class. The deserialized object properties have null value. Can somebody please guide me to use xml override correctly.

I have pasted the code here for trying in a console app.

public class Program
{
    private static void Main(string[] args)
    {
        var documentPropertyMapping =
            GetCustomSectionSettingList<CustomSectionConfigElement>("abc/xyz",
                "Activity", "value", "Function").ToDictionary(x => x.Key, x => x.Value);
            }

    private static List<T> GetCustomSectionSettingList<T>(string sectionName, string elementName, string keyAttributeName,
        string valueAttributeName) where T : new()
    {
        var xml = @"<root><abc>
    <xyz>
        <Activity value = ""a Document"" Function = ""a Documentation"" />
        <Activity value = ""b Document"" Function = ""a Documentation"" />
    </xyz>
</abc></root>";
        var settings = new List<T>();
        var configDoc = XElement.Parse(xml);

        var xOver = new XmlAttributeOverrides();
        {
            var attrs = new XmlAttributes();
            var root = new XmlRootAttribute(elementName);
            attrs.XmlRoot = root;
            xOver.Add(typeof (T), attrs);
        }
        {
            var attrs = new XmlAttributes();
            var attribute = new XmlElementAttribute(keyAttributeName);
            attrs.XmlElements.Add(attribute);
            xOver.Add(typeof (T), "Key", attrs);
        }
        {
            var attrs = new XmlAttributes();
            var attribute = new XmlElementAttribute();
            attribute.ElementName = valueAttributeName;
            attrs.XmlElements.Add(attribute);
            xOver.Add(typeof (T), "Value", attrs);
        }

        var serializer = new XmlSerializer(typeof (T), xOver);
        foreach (var sectionXml in configDoc.XPathSelectElements(sectionName))
        {
            foreach (var elem in sectionXml.Elements())
                settings.Add((T) serializer.Deserialize(elem.CreateReader()));
        }
        return settings;
    }

    #region custom section

    public class CustomSectionConfigElement
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }

    #endregion custom section
}
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
Jawahar
  • 117
  • 7
  • Try to look here: http://stackoverflow.com/questions/279534/proper-way-to-implement-ixmlserializable – Oxoron Oct 29 '15 at 17:27

1 Answers1

1

The problem is you use XmlElementAttribute instead of XmlAttributeAttribute. The first means that a field or property is serialized as a nested XML element, and the second as an attribute of the enclosing XMl element.

Hence the overrides should look like this:

{
    var attrs = new XmlAttributes();
    attrs.XmlAttribute = new XmlAttributeAttribute(keyAttributeName);
    xOver.Add(typeof (T), "Key", attrs);
}
{
    var attrs = new XmlAttributes();
    attrs.XmlAttribute = new XmlAttributeAttribute(valueAttributeName);
    xOver.Add(typeof (T), "Value", attrs);
}

Note that your original code would work for XML like this:

<Activity>
    <value>a Document</value>
    <Function>a Documentation</Function>
</Activity>
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90