具有继承功能的自定义XML序列化 [英] Custom XML serialization with inheritance

查看:79
本文介绍了具有继承功能的自定义XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下要序列化的类结构:

I have the following class structure which I want to serialize:

    [XmlRoot("SomeClass")]
public class SomeClass
{
    private BaseClass[] _itemArray;
    public BaseClass[] ItemArray
    {
        get { return _itemArray; }
        set { _itemArray = value; }
    }

    public PPSPStatReportMessage()
        : base()
    {
    }
}

public class SomeOtherClass
{
    private int _property5;
    [XmlAttribute("Property5")]
    public int Property5
    {
        get { return _property5; }
        set { _property5 = value; }
    }

    private string _property6;
    [XmlText()]
    public string Property6
    {
        get { return _property6; }
        set { _property6 = value; }
    }

    public SomeOtherClass()
    {
    }
}

[XmlInclude(typeof(DerivedClass1))]
[XmlInclude(typeof(DerivedClass2))]
[XmlRoot("BaseClass")]
[XmlType("")]
public class BaseClass
{
    private string _property1;
    [XmlAttribute("Property1")]
    public string Property1
    {
        get { return _property1; }
        set { _property1 = value; }
    }

    public SomeClass(string PropertyVal)
    {
        _property1 = PropertyVal;
    }
}

[XmlRoot("BaseClass")]
[XmlTypeAttribute(Namespace = "")]
public class DerivedClass1 : BaseClass
{
    private string _property2;
    [XmlAttribute("Property2")]
    public string Property2
    {
        get { return _property2; }
        set { _property2 = value; }
    }


    private SomeOtherClass _property3;
    [XmlElement("SomeOtherClass")]
    public SomeOtherClass Property3
    {
        get { return _property3; }
        set { _property3 = value; }
    }

    public DerivedClass()
        : base("PropertyVal1")
    {
    }
}

[XmlRoot("BaseClass", Namespace = "")]
[XmlType("")]
public class DerivedClass2 : BaseClass
{
    private Int64 _property4;
    [XmlAttribute("Property4")]
    public Int64 Property4
    {
        get { return _property4; }
        set { _property4 = value; }
    }

    public DerivedClass2()
        : base("PropertyVal2")
    {
    }
}

这是我用来序列化SomeClass的方法:

And this is the method I use to serialize SomeClass:

public static string SerializeXML(object Value, System.Type ObjectType)
    {
        XmlSerializer serializer = new XmlSerializer(ObjectType);
        XmlSerializerNamespaces namespaceSerializer = new XmlSerializerNamespaces();
        namespaceSerializer.Add("", "");
        StringWriter ms = new StringWriter();
        serializer.Serialize(ms, Value, namespaceSerializer);
        return ms.ToString();
    }

此方法生成一个如下所示的XML结构:

This method generates an XML structure that looks like the following:

<?xml version="1.0" encoding="utf-16"?>
<SomeClass>
  <ItemArray>
    <BaseClass d3p1:type="DerivedClass1" Property1="PropertyVal1" Property2="123" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance">
      <SomeOtherClass Property5="0" >STRING DATA</SomeOtherClass>
    </BaseClass>
    <BaseClass d3p1:type="DerivedClass2" Property="PropertyVal2" Property4="456" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" />
  </ItemArray>
</SomeClass>

但是,我需要省略d3p1:type和xmlns:d3p1属性,并生成一个如下所示的XML结构:

However, I need to omit the d3p1:type and xmlns:d3p1 attributes and generate an XML structure that looks like this:

<?xml version="1.0" encoding="utf-16"?>
<SomeClass>
  <ItemArray>
    <BaseClass Property1="PropertyVal1" Property2="123">
      <SomeOtherClass Property5="0" >STRING DATA</SomeOtherClass>
    </BaseClass>
    <BaseClass Property="PropertyVal2" Property4="456" />
  </ItemArray>
</SomeClass>

您可以在代码中看到,我尝试使用XmlType和XmlTypeAttribute,但没有成功.

As you can see in the code, I've tried to use XmlType and XmlTypeAttribute, but without success.

关于如何生成上述XML结构(没有d3p1:type和xmlns:d3p1属性)的任何建议吗?

Any advice on how can I generate the XML structure as described above (without d3p1:type and xmlns:d3p1 attributes)?

推荐答案

您是否需要将子类元素称为"BaseClass"-否则派生类的名称就可以了吗?

Do you need the subclass elements to be called "BaseClass" - or would the name of the derived class do?

我正在序列化类似的子类结构,并且还希望摆脱"d3p1:type"和"xmlns:d3p1"标签-而是用派生类标签替换"BaseClass"标签.因此可以为您的示例生成xml:

I am serializing similar subclass structures, and also wanted to get rid of the "d3p1:type" and "xmlns:d3p1" tags - instead replacing the "BaseClass" tags with derived class tags. So it is possible to generate xml for your example:

<ItemArray>
    <DerivedClass1 Property1="PropertyVal1" Property2="123">
        ....
    </DerivedClass1>

您正在BaseClass上使用XmlInclude属性,以使Serliazer知道需要哪些派生类.相反,您可以将每个元素上预期的子类型告诉序列化器:

You're using an XmlInclude attribute on your BaseClass to let the serliazer know which derived classes to expect. Instead, you can tell the serializer about the expected subtypes on each element:

public class SomeClass
{
    private BaseClass[] _itemArray;

    [XmlElement(typeof(DerivedClass1))]
    [XmlElement(typeof(DerivedClass2))]
    public BaseClass[] ItemArray
    { 
        get { return _itemArray; }
        set { _itemArray = value; }
}

消除类型"属性.反序列化也可以正常工作.

Eliminating the "type" attributes. Deserialiazation will also work fine.

这篇关于具有继承功能的自定义XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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