反序列化派生类时的 XmlSerializer 异常(不需要 <Derived xmlns=''>) [英] XmlSerializer exception when deserializing derived class (&lt;Derived xmlns=&#39;&#39;&gt; was not expected)

查看:96
本文介绍了反序列化派生类时的 XmlSerializer 异常(不需要 <Derived xmlns=''>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XmlSerializer 序列化和反序列化类的层次结构.序列化工作正常,但是当我尝试反序列化时,出现此异常:

I am trying to serialize and deserialize a hierarchy of classes using XmlSerializer. The serialization works fine, but when I try to deserialize I get this exception:

System.InvalidOperationException: XML 文档 (2, 2) 中存在错误.---> System.InvalidOperationException: 不是预期的.

System.InvalidOperationException: There is an error in XML document (2, 2). ---> System.InvalidOperationException: <Derived xmlns=''> was not expected.

这是我要反序列化的 xml:

This is the xml that I am trying to deserialize:

<?xml version="1.0"?>
<Derived xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <BaseStr>Base</BaseStr>
  <DerivedStr>Derived</DerivedStr>
</Derived>

这是我正在使用的代码:

This is the code that I am using:

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

[XmlInclude(typeof(Derived))]
public abstract class Base 
{
    public string BaseStr { get; set; }
}

public class Derived : Base
{
    public string DerivedStr { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Derived));
            MemoryStream ms = new MemoryStream();
            serializer.Serialize(ms, new Derived() { DerivedStr = "Derived", BaseStr = "Base" });

            Console.WriteLine(Encoding.ASCII.GetChars(ms.ToArray()));

            ms.Position = 0;

            XmlSerializer deserializer = new XmlSerializer(typeof(Base));
            Base b = (Base)deserializer.Deserialize(ms);
            Console.WriteLine(b.GetType().Name);
        }
        catch(Exception ex)
        {
            Console.WriteLine();
            Console.WriteLine(ex.ToString());
        }
    }
}

为什么反序列化不起作用?我怎样才能让它工作?

Why does the deserialization not work? How can I get it to work?

推荐答案

您正在创建一个 XmlSerializer,它期望反序列化 Base对象.它不希望看到 元素.如果你使用

You're creating an XmlSerializer which expects to deserialize just Base objects. It doesn't expect to see a <Derived> element. If you use

XmlSerializer deserializer = new XmlSerializer(typeof(Derived));

你没有得到例外.或者,当您序列化Derived 的实例时,您可以使用一个序列化程序,它会生成一个 元素:

you don't get the exception. Alternatively, when you serialize the instance of Derived, you can use a serializer which will produce a <Base> element:

XmlSerializer serializer = new XmlSerializer(typeof(Base));

这仍然会反序列化为 Derived 的实例,因为 XML 将包括

That will still deserialize to an instance of Derived, because the XML will include

xsi:type="Derived"

基本上你的序列化器和反序列化器需要用相同的类型构造.

Basically your serializer and deserializer need to be constructed with the same type.

这篇关于反序列化派生类时的 XmlSerializer 异常(不需要 <Derived xmlns=''>)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆