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

    [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的方法:

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结构如下所示:

<?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结构:

<?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,但是没有成功。

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

最佳答案

您是否需要将子类元素称为“ BaseClass”-或派生类的名称呢?

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

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


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

public class SomeClass
{
    private BaseClass[] _itemArray;

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


消除“类型”属性。反序列化也可以正常工作。

08-05 16:35