22
我发现了一个问题,C#的XML序列化。串行化器的输出是正常的Win32和WinCE(但令人惊讶的WinCE具有IMO correcter输出)之间不一致。 Win32的根本忽略了Class2中XmlRoot("c2")
属性。C#阵列XML序列化
有谁知道路怎么走在WinCE像在Win32输出(因为我不希望XML标记有序列化类的类名)。
测试代码:
using System;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleTest
{
[Serializable]
[XmlRoot("c1")]
public class Class1
{
[XmlArray("items")]
public Class2[] Items;
}
[Serializable]
[XmlRoot("c2")]
public class Class2
{
[XmlAttribute("name")]
public string Name;
}
class SerTest
{
public void Execute()
{
XmlSerializer ser = new XmlSerializer(typeof (Class1));
Class1 test = new Class1 {Items = new [] {new Class2 {Name = "Some Name"}, new Class2 {Name = "Another Name"}}};
using (TextWriter writer = new StreamWriter("test.xml"))
{
ser.Serialize(writer, test);
}
}
}
}
预期的XML(WinCE的生成此):
<?xml version="1.0" encoding="utf-8"?>
<c1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<items>
<c2 name="Some Name" />
<c2 name="Another Name" />
</items>
</c1>
的Win32 XML(似乎是错误的版本):
<?xml version="1.0" encoding="utf-8"?>
<c1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<items>
<Class2 name="Some Name" />
<Class2 name="Another Name" />
</items>
</c1>