gpt4 book ai didi

c# - 带有继承的自定义 XML 序列化

转载 作者:太空宇宙 更新时间:2023-11-03 20:35:37 26 4
gpt4 key购买 nike

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

    [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; }
}

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

关于c# - 带有继承的自定义 XML 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5170300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com