public object DeserializeFromXml(Type targetType, string xmlString, Encoding encoding = null) { if (targetType == null) { throw new ArgumentNullException(nameof(targetType)); } if (string.IsNullOrEmpty(xmlString)) { throw new ArgumentException("Must not be null or empty", nameof(xmlString)); } encoding = encoding ?? this.DefaultEncoding; byte[] buffer = encoding.GetBytes(xmlString); if (!ValueToTypeMapping.CheckIfStringContainsTypeInformation(xmlString)) { var serializer = new XmlSerializer(targetType); using (var memoryStream = new MemoryStream(buffer)) { var deserialized = serializer.Deserialize(memoryStream); return(deserialized); } } bool isTargetTypeAnInterface = targetType.GetTypeInfo().IsInterface; Type[] extraTypes = { }; if (!isTargetTypeAnInterface) { extraTypes = new[] { targetType }; } var serializerBefore = new XmlSerializer(typeof(ValueToTypeMapping), extraTypes); ValueToTypeMapping deserializedObject = null; using (var memoryStream = new MemoryStream(buffer)) { deserializedObject = (ValueToTypeMapping)serializerBefore.Deserialize(memoryStream); } // If the target type is an interface, we need to deserialize again with more type information if (isTargetTypeAnInterface) { // 依靠 ValueToTypeMapping 中的 TypeName 判断真实类型 Type serializedType = Type.GetType(deserializedObject.TypeName); // 重新反序列化 var serializerAfter = new XmlSerializer(typeof(ValueToTypeMapping), new[] { serializedType }); using (var memoryStream = new MemoryStream(buffer)) { deserializedObject = (ValueToTypeMapping)serializerAfter.Deserialize(memoryStream); } return(Convert.ChangeType(deserializedObject.Value, serializedType)); } return(deserializedObject.Value); }
public string SerializeToXml(Type sourceType, object value, bool preserveTypeInformation = false, Encoding encoding = null) { encoding = encoding ?? this.DefaultEncoding; if (sourceType.GetTypeInfo().IsInterface&& value != null) { sourceType = value.GetType(); } object objectToSerialize; if (preserveTypeInformation) { objectToSerialize = new ValueToTypeMapping { Value = value, TypeName = sourceType.FullName //TypeName = sourceType.Name // 必须全名,否则找不到 }; } else { objectToSerialize = value; } var mainType = objectToSerialize?.GetType() ?? sourceType; var extraTypes = new[] { sourceType }; var serializer = new XmlSerializer(mainType, extraTypes); // 去掉 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 属性 XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces( new XmlQualifiedName[] { XmlQualifiedName.Empty }); using (var memoryStream = new MemoryStream()) { using (var streamWriter = new StreamWriter(memoryStream, encoding)) { serializer.Serialize(streamWriter, objectToSerialize, namespaces); byte[] buffer = (streamWriter.BaseStream as MemoryStream).ToArray(); string xml = encoding.GetString(buffer, 0, buffer.Length); return(xml); } } }
/// <inheritdoc /> public string SerializeToXml(Type sourceType, object value, bool preserveTypeInformation = false, Encoding encoding = null) { encoding = encoding ?? this.Encoding; if (sourceType.GetTypeInfo().IsInterface&& value != null) { sourceType = value.GetType(); } object objectToSerialize; if (preserveTypeInformation) { objectToSerialize = new ValueToTypeMapping { Value = value, TypeName = sourceType.FullName }; } else { objectToSerialize = value; } var mainType = objectToSerialize?.GetType() ?? sourceType; var extraTypes = new[] { sourceType }; var serializer = new XmlSerializer(mainType, extraTypes); using (var memoryStream = new MemoryStream()) { using (var streamWriter = new StreamWriter(memoryStream, encoding)) { serializer.Serialize(streamWriter, objectToSerialize); return(ByteConverter.GetStringFromByteArray(encoding, ((MemoryStream)streamWriter.BaseStream).ToArray())); } } }