C#阵列XML序列化 [英] C# Array XML Serialization

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

问题描述

我发现了一个问题,C#的XML序列化。串行器的输出是正常的Win32和WinCE(但令人惊讶的WinCE的有海事组织correcter输出)之间的不一致。 Win32的简单忽略的Class2 XmlRoot(C2)属性。



有谁知道路怎么走的。WinCE的输出一样在Win32(因为我不希望XML标记有序列化类的类名)



测试代码:

 使用系统; 
使用的System.Xml.Serialization;
:使用System.IO;

命名空间ConsoleTest
{
[Serializable接口]
[XmlRoot(C1)]
公共类的Class1
{
[XmlArray(项目)]
公共类2 []项目;
}

[Serializable接口]
[XmlRoot(C2)]
公共类Class2中
{
[XmlAttribute(名 )
公共字符串名称;
}

类SerTest
{
公共无效执行()
{
XmlSerializer的SER =新的XmlSerializer(typeof运算(1级));

Class1的测试=新1级{项目=新的[] {新的Class2 {名称=一些名称},新的Class2 {名称=另一个名称}}};使用

(TextWriter的作家=新的StreamWriter(的test.xml))
{
ser.Serialize(作家,试);
}
}
}
}



预期XML(WinCE的生成此):??

 < XML版本=1.0编码=UTF-8> 
< C1的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema>
<项目>
< C2名=一些名/>
< C2 NAME =另一名/>
< /项目>
< / C1>



Win32的XML(似乎是错误的版本):

 <?XML版本=1.0编码=UTF-8>?; 
< C1的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema>
<项目>
<类2名=一些名/>
<类2名=另一名/>
< /项目>
< / C1>


解决方案

尝试[XmlArrayItem(C2)]

  [XmlRoot(C1)] 
公共类的Class1
{
[XmlArray(项目)]
[XmlArrayItem(C2)]
公共类2 []项目;
}

或[XmlType将(C2)]



  [XmlType将(C2)] 
公共类Class2中
{
[XmlAttribute(名称)]
酒店的公共字符串名称;
}


I found a problem with the XML Serialization of C#. The output of the serializer is inconsistent between normal Win32 and WinCE (but surprisingly WinCE has the IMO correcter output). Win32 simply ignores the Class2 XmlRoot("c2") Attribute.

Does anyone know a way how to get the WinCE like output on Win32 (because i don't want the XML tags to have the class name of the serialization class).

Test Code:

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

Expected XML (WinCE generates this):

<?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 (seems to be the wrong version):

<?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>

解决方案

Try [XmlArrayItem("c2")]

[XmlRoot("c1")]
public class Class1
{
    [XmlArray("items")]
    [XmlArrayItem("c2")] 
    public Class2[] Items;
}

or [XmlType("c2")]

[XmlType("c2")]
public class Class2
{
    [XmlAttribute("name")]
    public string Name;
}

这篇关于C#阵列XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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