如何反序列化xml忽略节点类型的差异,即成员是表示为XmlElement还是表示为XmlAttribute。
例如,我有这个源xml
<Book>
<Title>Introduction to c#</Title>
<Publisher>John Smith</Publisher>
<Year>2012</Year>
<Book>
我想用这个类反序列化它
public class Book
{
public string Title{ get; set; }
[XmlAttribute()]
public string Publisher{ get; set; }
[XmlAttribute()]
public string Year{ get; set; }
}
正如您所看到的,Publisher和Year在类中标有“[XmlAttribute]”,因此它们不会被反序列化。
有没有办法告诉反序列化器忽略成员是表示为属性还是元素的差异?
我需要在通用转换器中将其转换为另一种类型
public static Type2 ConvertType1ToType2<Type1, Type2>(Type1 type1)
{
using (MemoryStream objectStream = new MemoryStream())
{
XmlSerializer type1Serializer = new XmlSerializer(typeof(Type1));
XmlSerializer type2Deserializer = new XmlSerializer(typeof(Type2));
type1Serializer.Serialize(objectStream, type1);
objectStream.Position = 0;
return (Type2)type2Deserializer.Deserialize(objectStream);
}
}
答案 0 :(得分:1)
试试这个,
string sXml = @"<Book>
<Title>Introduction to c#</Title>
<Publisher>John Smith</Publisher>
<Year>2012</Year>
</Book>";
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes PublisherAttributes = new XmlAttributes();
XmlAttributes YearAttributes = new XmlAttributes();
PublisherAttributes.XmlElements.Add(new XmlElementAttribute("Publisher"));
YearAttributes.XmlElements.Add(new XmlElementAttribute("Year"));
overrides.Add(typeof(Book), "Publisher", PublisherAttributes);
overrides.Add(typeof(Book), "Year", YearAttributes);
XmlSerializer ser = new XmlSerializer(typeof(Book), overrides);
System.IO.TextReader oReader = new System.IO.StringReader(sXml);
Book oBook = (Book) ser.Deserialize(oReader);
这有帮助吗?
答案 1 :(得分:1)
在更新问题时,需要使用通用逻辑将Type1转换为Type2。
尝试以下解决方案,
对于两个类型类(Type1&amp; Type2),我假设它们都具有相同的同名属性。
让我们说我们有两个班级的书&amp;第一册。
public class Book
{
public string Title { get; set; }
[XmlAttribute()]
public string Publisher { get; set; }
[XmlAttribute()]
public string Year { get; set; }
}
public class Book1
{
public string Title { get; set; }
[XmlAttribute()]
public string Publisher { get; set; }
[XmlAttribute()]
public string Year { get; set; }
}
ConvertType1ToType2功能:
public static Type2 ConvertType1ToType2<Type1, Type2>(Type1 type1)
{
using (System.IO.MemoryStream objectStream = new System.IO.MemoryStream())
{
XmlRootAttribute root = new XmlRootAttribute("Book");
//Get All MemberInfo of Type1
Type objType1 = type1.GetType();
System.Reflection.MemberInfo[] objType1Member = objType1.GetMembers();
List<Type> extraTypesForType1 = new List<Type>();
//Type1 => XmlAttributeOverrides
XmlAttributeOverrides Type1overrides = new XmlAttributeOverrides();
foreach (System.Reflection.MemberInfo m in objType1Member)
{
if (m.MemberType.Equals( System.Reflection.MemberTypes.Property))
{
XmlAttributes attributes = new XmlAttributes();
attributes.XmlElements.Add(new XmlElementAttribute(m.Name));
Type1overrides.Add(typeof(Type1), m.Name, attributes);
extraTypesForType1.Add(m.MemberType.GetType());
}
}
XmlSerializer type1Serializer = new XmlSerializer(typeof(Type1), Type1overrides, extraTypesForType1.ToArray(),root,"");
//Type2 => XmlAttributeOverrides
Type objType2 = type1.GetType();
System.Reflection.MemberInfo[] objType2Member = objType1.GetMembers();
List<Type> extraTypesForType2 = new List<Type>();
XmlAttributeOverrides Type2overrides = new XmlAttributeOverrides();
foreach (System.Reflection.MemberInfo m in objType2Member)
{
if (m.MemberType.Equals(System.Reflection.MemberTypes.Property))
{
XmlAttributes attributes = new XmlAttributes();
attributes.XmlElements.Add(new XmlElementAttribute(m.Name));
Type2overrides.Add(typeof(Type2), m.Name, attributes);
extraTypesForType2.Add(m.MemberType.GetType());
}
}
XmlSerializer type2Deserializer = new XmlSerializer(typeof(Type2), Type2overrides, extraTypesForType2.ToArray(),root,"");
type1Serializer.Serialize(objectStream, type1);
objectStream.Position = 0;
Type2 t = (Type2)type2Deserializer.Deserialize(objectStream);
return t;
}
现在创建Book类Object并调用函数ConvertType1ToType2。
Book book = new Book();
book.Title = "Introduction to c#";
book.Publisher = "John Smith";
book.Year = "2012";
Book1 book1 = ConvertType1ToType2<Book, Book1>(book);
希望这个解决方案可行。