0

如何反序列化 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);
            }
        }
4

2 回答 2

1

当您更新问题时,您需要将 Type1 转换为 Type2 的通用逻辑。

尝试以下解决方案,

对于两个类型类(Type1 和 Type2),我假设它们都具有相同名称的相同属性。

假设我们有两个班级 Book 和 Book1。

   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 类对象并调用函数 ConvertType1ToType2。

Book book = new Book();
            book.Title = "Introduction to c#";
            book.Publisher = "John Smith";
            book.Year = "2012";

 Book1 book1 = ConvertType1ToType2<Book, Book1>(book);

希望这个解决方案可以奏效。

于 2012-09-09T03:17:56.177 回答
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);

这有帮助吗?

于 2012-09-08T13:10:24.427 回答