How do you deserialize xml ignoring difference in node types, that is whether a member is represented as an XmlElement or as an XmlAttribute.
For example, I have this source xml
<Book>
<Title>Introduction to c#</Title>
<Publisher>John Smith</Publisher>
<Year>2012</Year>
<Book>
And I would like to deserialize it using this class
public class Book
{
public string Title{ get; set; }
[XmlAttribute()]
public string Publisher{ get; set; }
[XmlAttribute()]
public string Year{ get; set; }
}
As you can see Publisher and Year are marked with "[XmlAttribute]" in the class so they are not deserialized.
Is there a way to tell the deserializer to ignore differences in whether the member is represented as an Attribute or as an Element?
I need this in a generic Converter to convert one type to another similar type
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);
}
}