You can use the XmlSerializer.UnknownElement
event on XmlSerializer
to capture out-of-order elements. This will allow you to manually find and fix problems in deserialization.
A more complex answer would be to correctly order your elements when serializing, but ignore order when deserializing. This requires using the XmlAttributes
class and the XmlSerializer(Type,?XmlAttributeOverrides)
constructor. Note that serializers constructed in this manner must be cached in a hash table and resused to avoid a severe memory leak, and thus this solution is a little "finicky" since Microsoft doesn't provide a meaningful GetHashCode()
for XmlAttributeOverrides
. The following is one possible implementation which depends upon knowing in advance all types that need their XmlElementAttribute.Order
and XmlArrayAttribute.Order
properties ignored, thus avoiding the need to create a complex custom hashing method:
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;
}
}
}
(Note -- only partially tested.)
Then when deserializing a type, use the XmlSerializer
provided by the factory. Given that SoapEnvelope
is a subclass of XmlDocument
, you should be able to deserialize the body node along the lines of the answer in Deserialize object property with StringReader vs XmlNodeReader.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…