请查看下面的代码,它将一个包含3个对象列表的简单类写入XML文件。列表中的3个对象是从彼此派生的:Base、Derived1和Derived2。我使用XMLArrayItemAttributes在序列化期间覆盖名称。这在.NET 3.0中运行良好,但在.NET 4.0中输出了不同的结果。请参见下面的输出,特别注意第二个后代项DerivedItem2。
有没有人对此有经验,并知道如何在.NET 4.0中修复它,使其像v3.5一样工作?
似乎我无法控制数组项被覆盖的顺序。它似乎不是它们添加到XMLArrayItems中的顺序。
编辑:我刚刚尝试使用MONO针对框架版本4.0和4.5执行相同的示例,它们可以正常工作。这只是Microsoft框架版本的一个错误吗?
更新:.NET 4.5 的输出
更新: 在app.config中打开以下调试开关,参考自http://msdn.microsoft.com/en-us/library/aa302290.aspx,我发现序列化应用覆盖的顺序与我填写覆盖数组的顺序不同。有人知道如何确定或覆盖这个顺序吗?
这给我提供了一个C#输出文件,显示覆盖顺序不正确:
有没有人对此有经验,并知道如何在.NET 4.0中修复它,使其像v3.5一样工作?
似乎我无法控制数组项被覆盖的顺序。它似乎不是它们添加到XMLArrayItems中的顺序。
编辑:我刚刚尝试使用MONO针对框架版本4.0和4.5执行相同的示例,它们可以正常工作。这只是Microsoft框架版本的一个错误吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TestGroup g = new TestGroup();
XmlSerializer s = new XmlSerializer(typeof(TestGroup), g.GetOverrides());
TextWriter w = new StreamWriter("c:\\#\\test.xml");
s.Serialize(w, g);
w.Close();
}
}
public class TestGroup
{
public List<BaseItem> Items { get; set; }
public TestGroup()
{
Items = new List<BaseItem>();
BaseItem b = new BaseItem();
b.BaseName = "Base Name";
Items.Add(b);
DerivedItem d1 = new DerivedItem();
d1.BaseName = "D1";
d1.DerivedName = "D1";
Items.Add(d1);
DerivedItem2 d2 = new DerivedItem2();
d2.BaseName = "D2";
//d2.DerivedName = "D2";
d2.Derived2Name = "D2";
Items.Add(d2);
}
public XmlAttributeOverrides GetOverrides()
{
XmlAttributes atts = new XmlAttributes();
for (int i = 0; i < Items.Count; i++)
{
BaseItem b = Items[i];
Type ItemType = b.GetType();
XmlArrayItemAttribute ItemAtt = new XmlArrayItemAttribute();
ItemAtt.ElementName = ItemType.Name;
ItemAtt.Type = ItemType;
atts.XmlArrayItems.Add(ItemAtt);
}
XmlAttributeOverrides attOvers = new XmlAttributeOverrides();
attOvers.Add(typeof(TestGroup), "Items", atts);
return attOvers;
}
}
public class BaseItem : IXmlSerializable
{
public string BaseName;
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
// not required for example
}
public virtual void WriteXml(XmlWriter writer)
{
writer.WriteElementString("BaseName", this.BaseName);
}
}
public class DerivedItem: BaseItem
{
public string DerivedName;
public override void WriteXml(XmlWriter writer)
{
base.WriteXml(writer);
writer.WriteElementString("DerivedName", this.DerivedName);
}
}
public class DerivedItem2: DerivedItem
{
public string Derived2Name;
public override void WriteXml(XmlWriter writer)
{
base.WriteXml(writer);
writer.WriteElementString("Derived2Name", this.Derived2Name);
}
}
输出原始内容 (.NET 3.0):
<?xml version="1.0" encoding="utf-8"?>
<TestGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<BaseItem>
<BaseName>Base Name</BaseName>
</BaseItem>
<DerivedItem>
<BaseName>D1</BaseName>
<DerivedName>D1</DerivedName>
</DerivedItem>
<DerivedItem2>
<BaseName>D2</BaseName>
<DerivedName />
<Derived2Name>D2</Derived2Name>
</DerivedItem2>
</Items>
</TestGroup>
输出已更改(.NET 4.0):
<?xml version="1.0" encoding="utf-8"?>
<TestGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<BaseItem>
<BaseName>Base Name</BaseName>
</BaseItem>
<DerivedItem>
<BaseName>D1</BaseName>
<DerivedName>D1</DerivedName>
</DerivedItem>
<DerivedItem>
<BaseName>D2</BaseName>
<DerivedName />
<Derived2Name>D2</Derived2Name>
</DerivedItem>
</Items>
</TestGroup>
更新:.NET 4.5 的输出
<?xml version="1.0" encoding="utf-8"?>
<TestGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<BaseItem>
<BaseName>Base Name</BaseName>
</BaseItem>
<BaseItem>
<BaseName>D1</BaseName>
<DerivedName>D1</DerivedName>
</BaseItem>
<DerivedItem2>
<BaseName>D2</BaseName>
<DerivedName />
<Derived2Name>D2</Derived2Name>
</DerivedItem2>
</Items>
</TestGroup>
更新: 在app.config中打开以下调试开关,参考自http://msdn.microsoft.com/en-us/library/aa302290.aspx,我发现序列化应用覆盖的顺序与我填写覆盖数组的顺序不同。有人知道如何确定或覆盖这个顺序吗?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<switches>
<add name="XmlSerialization.Compilation" value="4" />
</switches>
</system.diagnostics>
</configuration>
这给我提供了一个C#输出文件,显示覆盖顺序不正确:
void Write2_TestGroup(string n, string ns, global::WindowsFormsApplication1.TestGroup o, bool isNullable, bool needType) {
if ((object)o == null) {
if (isNullable) WriteNullTagLiteral(n, ns);
return;
}
if (!needType) {
System.Type t = o.GetType();
if (t == typeof(global::WindowsFormsApplication1.TestGroup)) {
}
else {
throw CreateUnknownTypeException(o);
}
}
WriteStartElement(n, ns, o, false, null);
if (needType) WriteXsiType(@"TestGroup", @"");
{
global::System.Collections.Generic.List<global::WindowsFormsApplication1.BaseItem> a = (global::System.Collections.Generic.List<global::WindowsFormsApplication1.BaseItem>)((global::System.Collections.Generic.List<global::WindowsFormsApplication1.BaseItem>)o.@Items);
if (a != null){
WriteStartElement(@"Items", @"", null, false);
for (int ia = 0; ia < ((System.Collections.ICollection)a).Count; ia++) {
global::WindowsFormsApplication1.BaseItem ai = (global::WindowsFormsApplication1.BaseItem)a[ia];
if ((object)(ai) != null){
if (ai is global::WindowsFormsApplication1.DerivedItem) {
WriteSerializable((System.Xml.Serialization.IXmlSerializable)((global::WindowsFormsApplication1.DerivedItem)ai), @"DerivedItem", @"", true, true);
}
else if (ai is global::WindowsFormsApplication1.BaseItem) {
WriteSerializable((System.Xml.Serialization.IXmlSerializable)((global::WindowsFormsApplication1.BaseItem)ai), @"BaseItem", @"", true, true);
}
else if (ai is global::WindowsFormsApplication1.DerivedItem2) {
WriteSerializable((System.Xml.Serialization.IXmlSerializable)((global::WindowsFormsApplication1.DerivedItem2)ai), @"DerivedItem2", @"", true, true);
}
else if ((object)(ai) != null){
throw CreateUnknownTypeException(ai);
}
}
}
WriteEndElement();
}
}
WriteEndElement(o);
}
WriteXml
和GetSchema
并改用属性来控制序列化Items
时,是否会出现相同的症状?即仅使用默认序列化而没有覆盖。如果是这样,那么它似乎是一个明显的XmlSerializer错误,因为无法从生成的XML反序列化,您只能向MS报告错误。如果这不会产生问题,则从那里构建到您当前的复制代码,并查看问题出现的位置。我怀疑GetOverrides()
中的某些内容是罪魁祸首。 - RoryWriteXML
仅用于正确写出对象类型的属性。 - Pat Buxton