【问题标题】:Deserializing XML with unknown element order使用未知元素顺序反序列化 XML
【发布时间】:2016-02-04 01:16:59
【问题描述】:

我正在尝试为规范确实不足的服务实现客户端。它类似于 SOAP,尽管它没有 WSDL 或等效文件。该规范也没有提供有关元素正确排序的任何信息 - 它们在规范中按字母顺序列出,但如果它们在请求中无序(所述顺序由检查示例)。

我可以使用这个来提交请求,即使这很痛苦。但是,我不知道如何正确处理响应。

同时使用SoapEnvelope 和直接使用XmlSerializer,如果响应包含我尚未正确订购的元素,它会在我的对象上显示为null。再一次,我可以设法使用它,并手动排序类属性with Order attributes,但我无法判断原始 XML 是否有一个我没有正确排序的字段,因此被保留为 null .

这让我想到了当前的问题:如何检查 XmlSerializer 是否删除了字段

【问题讨论】:

  • 我真的希望这实际上是一个 X-Y 问题,并且有一个比检查 XmlSerializer 更简单的答案,但我并不期待。
  • 你可以使用XmlSerializer.UnknownElement
  • @dbc - 哦,这可以工作。这并不理想,但我绝对可以使用它。当然,这种情况没有什么是理想的,所以我不应该感到惊讶。
  • @dbc - 实际上,在您将其转化为答案之前,有没有办法将其作为SoapEnvelope.GetBodyObject() 调用的一部分,还是我需要直接使用 XmlSerializer?
  • 只是为了确保我理解:序列化时,您想为类型中的每个属性设置XmlElementAttribute.OrderXmlArrayAttribute.Order,但是在反序列化时,您想忽略顺序并反序列化中的元素有什么命令吗?

标签: c# serialization soap xml-serialization xmlserializer


【解决方案1】:

您可以使用XmlSerializer 上的XmlSerializer.UnknownElement 事件来捕获乱序元素。这将允许您手动查找和修复反序列化中的问题。

更复杂的答案是在序列化时正确排序元素,但在反序列化时忽略顺序。这需要使用XmlAttributes 类和XmlSerializer(Type, XmlAttributeOverrides) 构造函数。请注意,以这种方式构造的序列化程序must be cached in a hash table and resused 是为了避免严重的内存泄漏,因此这个解决方案有点“挑剔”,因为微软没有为XmlAttributeOverrides 提供有意义的GetHashCode()。以下是一种可能的实现,它依赖于提前知道所有需要忽略其XmlElementAttribute.OrderXmlArrayAttribute.Order 属性的类型,从而避免创建复杂的自定义散列方法:

public class XmlSerializerFactory : XmlOrderFreeSerializerFactory
{
    static readonly XmlSerializerFactory instance;

    // Use a static constructor for lazy initialization.
    private XmlSerializerFactory()
        : base(new[] { typeof(Type2), typeof(Type1), typeof(TestClass), typeof(Type3) }) // These are the types in your client for which Order needs to be ignored whend deserializing
    {
    }

    static XmlSerializerFactory()
    {
        instance = new XmlSerializerFactory();
    }

    public static XmlSerializerFactory Instance { get { return instance; } }
}

public abstract class XmlOrderFreeSerializerFactory
{
    readonly XmlAttributeOverrides overrides;
    readonly object locker = new object();
    readonly Dictionary<Type, XmlSerializer> serializers = new Dictionary<Type, XmlSerializer>();

    static void AddOverrideAttributes(Type type, XmlAttributeOverrides overrides)
    {
        if (type == null || type == typeof(object) || type.IsPrimitive || type == typeof(string))
            return;

        var mask = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public;
        foreach (var member in type.GetProperties(mask).Cast<MemberInfo>().Union(type.GetFields(mask)))
        {
            XmlAttributes overrideAttr = null;
            foreach (var attr in member.GetCustomAttributes<XmlElementAttribute>())
            {
                overrideAttr = overrideAttr ?? new XmlAttributes();
                overrideAttr.XmlElements.Add(new XmlElementAttribute { DataType = attr.DataType, ElementName = attr.ElementName, Form = attr.Form, IsNullable = attr.IsNullable, Namespace = attr.Namespace, Type = attr.Type });
            }
            foreach (var attr in member.GetCustomAttributes<XmlArrayAttribute>())
            {
                overrideAttr = overrideAttr ?? new XmlAttributes();
                overrideAttr.XmlArray = new XmlArrayAttribute { ElementName = attr.ElementName, Form = attr.Form, IsNullable = attr.IsNullable, Namespace = attr.Namespace };
            }
            if (overrideAttr != null)
                overrides.Add(type, member.Name, overrideAttr);
        }
    }

    protected XmlOrderFreeSerializerFactory(IEnumerable<Type> types)
    {
        overrides = new XmlAttributeOverrides();
        foreach (var type in types.SelectMany(t => t.BaseTypesAndSelf()).Distinct())
        {
            AddOverrideAttributes(type, overrides);
        }
    }

    public XmlSerializer GetSerializer(Type type)
    {
        if (type == null)
            throw new ArgumentNullException("type");
        lock (locker)
        {
            XmlSerializer serializer;
            if (!serializers.TryGetValue(type, out serializer))
                serializers[type] = serializer = new XmlSerializer(type, overrides);
            return serializer;
        }
    }
}

public static class TypeExtensions
{
    public static IEnumerable<Type> BaseTypesAndSelf(this Type type)
    {
        while (type != null)
        {
            yield return type;
            type = type.BaseType;
        }
    }
}

(注意——仅部分测试。)

然后在反序列化一个类型的时候,使用工厂提供的XmlSerializer。鉴于SoapEnvelopeXmlDocument 的子类,您应该能够按照Deserialize object property with StringReader vs XmlNodeReader 中的答案对正文节点进行反序列化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    相关资源
    最近更新 更多