考虑这种类型:
[DataContract]
public class EntityId
{
[DataMember(Order = 1)]
public string IdAsString { get; set; }
[DataMember(Order = 2)]
public Type Type { get; set; }
}
我为它创建了一个Xml序列化程序集。但是,尝试序列化它会产生异常:
System.InvalidOperationException occurred
Message=The type NC.DTO.FlowFolder was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
Source=NC.DTO.XmlSerializers
StackTrace:
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_Type(String n, String ns, Type o, Boolean isNullable, Boolean needType)
InnerException:
其中 NC.DTO.FlowFolder 实际上是typeof(NC.DTO.FlowFolder)
的字符串表示形式 - 类型NC.DTO.FlowFolder
对于Xml序列化程序是已知的,它似乎无法序列化的是Type
对象本身。
反射器显示失败的方法内容:
private void Write3_Type(string n, string ns, Type o, bool isNullable, bool needType)
{
if (o == null)
{
if (isNullable)
{
base.WriteNullTagLiteral(n, ns);
}
}
else if (!needType && (o.GetType() != typeof(Type)))
{
throw base.CreateUnknownTypeException(o);
}
}
请注意throw
声明。
堆栈跟踪是:
NC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_Type(string n, string ns, System.Type o, bool isNullable, bool needType) + 0xda bytes
NC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write11_EntityId(string n, string ns, NC.DTO.EntityId o, bool isNullable, bool needType) + 0x2a8 bytes
NC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write14_FlowFolder(string n, string ns, NC.DTO.FlowFolder o, bool isNullable, bool needType) + 0x36b bytes
NC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write42_FlowFolder(object o) + 0xc9 bytes
[Native to Managed Transition]
System.Xml.dll!System.Xml.Serialization.TempAssembly.InvokeWriter(System.Xml.Serialization.XmlMapping mapping, System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle, string id) Line 342 + 0xb9 bytes C#
System.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle, string id) Line 676 + 0xdf bytes C#
System.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle) Line 646 C#
System.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces) Line 640 C#
System.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o) Line 616 C#
我的问题是如何让Xml序列化程序更乐于序列化和反序列化EntityId
个对象?
感谢。
P.S。
如果有人知道另一个Xml序列化库,那么将XML序列化为Newtonsoft.Json到JSON序列化 - 请分享。
答案 0 :(得分:5)
xml通常旨在与平台无关,因此无法正常工作。你可以尝试:
[XmlIgnore]
public Type Type { get; set; }
[XmlElement("Type")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public string AssemblyQualifiedTypeName
{
get { return Type == null ? null : Type.AssemblyQualifiedName; }
set { Type = string.IsNullOrEmpty(value) ? null : Type.GetType(value); }
}
另请注意,如果您使用XmlSerializer
,则数据协定属性不起作用。另请注意,这将使您与.NET以及可能的特定版本等相关联。