简体   繁体   English

具有派生类型的XML序列化

[英]XML-serialization with derived types

I´ve again a problem on getting my serializer to work. 在使我的序列化器正常工作时,我又遇到了一个问题。 I have a baseclass A and a derived class B: 我有一个基类A和一个派生类B:

class Program
{
    static void Main(string[] args)
    {
        A foo = new B();

        // determine that the class B overrides A
        XmlAttributeOverrides overrides = new XmlAttributeOverrides();
        overrides.Add(typeof(A), new XmlAttributes
        {
            XmlElements = { new XmlElementAttribute("B", typeof(B)) }
        });

        XmlSerializer ser = new XmlSerializer(typeof(A), overrides);
        ser.Serialize(new XmlTextWriter("test.xml", Encoding.Default), foo);

    }
}

public class A { public int a;}
public class B : A { public int b;}

But when I run this little program I get exception 但是当我运行这个小程序时,我得到了异常

XmlRoot and XmlType attributes may not be specified for the type ConsoleApplication1.A 可能未为ConsoleApplication1.A类型指定XmlRoot和XmlType属性

but I never determined the root or type-attribute for class A so I´m really confused about this message. 但是我从未确定类A的根或类型属性,因此我对此消息感到非常困惑。 Is there anything behind the scenes I have to specify? 我必须指定幕后内容吗? All I want do is serialize an instance of B that simply add some properties to the definition of A... 我想要做的就是序列化B的实例,该实例只需向A的定义中添加一些属性即可。

It is right there in the error message you are getting. 就在您收到的错误消息中。

XmlRoot and XmlType attributes may not be specified for the type ConsoleApplication1.A 可能未为ConsoleApplication1.A类型指定XmlRoot和XmlType属性

You have to specify a root element for the output xml document. 您必须为输出xml文档指定根元素。

Replace 更换

overrides.Add(typeof(A), new XmlAttributes

with

overrides.Add(typeof(A), "node", new XmlAttributes

And you will have to probably also replace 而且您可能还必须更换

new XmlSerializer(typeof(A), overrides);     

with

new XmlSerializer(typeof(B), overrides);

For more info and example on overriding, please visit MSDN . 有关覆盖的更多信息和示例,请访问MSDN

I finally got two solutions to work: 我终于有了两种解决方案:

Number 1: 1号:

...
XmlSerializer ser = new XmlSerializer(typeof(A));
ser.Serialize(new XmlTextWriter("test.xml", Encoding.Default), foo);
...         

[System.Xml.Serialization.XmlRoot("Root", Namespace = "DefaultNS")]
[System.Xml.Serialization.XmlInclude(typeof(B))]
public class A { public int a;}

[System.Xml.Serialization.XmlRoot(Namespace = "CustomNS")]
public class B : A { public int b;}

Number 2 (in addition to Ondrejs solution): 2号(除了Ondrejs解决方案):

...
XmlSerializer ser = new XmlSerializer(typeof(A), overrides, new[] { typeof(B) }, new XmlRootAttribute("Root"), "defautlNS");
...

public class A { public int a;}
public class B : A { public int b;}

The second one in contrast to the first solution has the disadvantage that you may not see where an attribute within the XML-document comes from (from defaultNS or customNS) since you cannot specify any customNS. 与第一种解决方案相比,第二种缺点是,由于无法指定任何customNS,因此您可能看不到XML文档中属性的来源(来自defaultNS或customNS)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM