【问题标题】:how to use XmlAttributeOverrides for deserializing in c#如何在 C# 中使用 XmlAttributeOverrides 进行反序列化
【发布时间】:2015-10-29 15:40:27
【问题描述】:

我正在尝试使用通用类反序列化自定义配置键。反序列化的对象属性具有空值。有人可以指导我正确使用 xml 覆盖。

我已在此处粘贴代码以在控制台应用中尝试。

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
}

【问题讨论】:

标签: c# xml deserialization xmlserializer


【解决方案1】:

问题是您使用XmlElementAttribute 而不是XmlAttributeAttribute。第一个表示字段或属性被序列化为嵌套的 XML 元素,第二个表示为封闭 XMl 元素的 attribute

因此覆盖应该如下所示:

{
    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);
}

请注意,您的 原始 代码适用于 XML,如下所示:

<Activity>
    <value>a Document</value>
    <Function>a Documentation</Function>
</Activity>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2015-07-30
    • 1970-01-01
    • 2012-03-13
    相关资源
    最近更新 更多