c# - List<Int> XML 序列化

标签 c# xml

List<int> testList = new List<int>();
testList.Add(1);
testList.Add(2);
testList.Add(3);

XmlSerializer xs = new XmlSerializer(typeof(List<int>));

此代码(部分)创建默认根节点 <ArrayOfInts>每个节点:<int> .

是否可以在不创建包装类的情况下设置不同的名称?

谢谢

最佳答案

您可以将 XmlArray 和 XMLArrayItem 属性一起用于变量声明的顶部。然后 XmlSerializer 在开始序列化定义的对象时考虑这些属性。让我用您的代码举个例子;

您应该使用这些属性定义通用列表。

public class democlass
{

    [XmlArray("testList")]
    [XmlArrayItem("customitem")]
    public List<int> testList {get;set;}
}

然后, 您可以将值添加到您的列表中

    static void Main(string[] args)
    {
        democlass d = new democlass();
        d.testList = new List<int>();
        d.testList.Add(1);
        d.testList.Add(2);
        d.testList.Add(3);

并序列化它。您将看到此输出。

<democlass>
    <testList>
        <customitem>1</customitem>
        <customitem>2</customitem>
        <customitem>3</customitem>
    </testList>
</democlass>

就是这样。

希望对您有所帮助。

注意:神奇的是 XmlArrayXmlArrayItem 属性,您可以在 MSDN 上找到更详细的信息。

问候

关于c# - List<Int> XML 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1075084/

相关文章:

c# - Memcached 与 Windows 和 .NET

android - 在android中绘制自定义形状

javascript - 从 HTML 页面插入 XML

xml - 最佳实践 : XML attribute vs XML element - When should I use elements and when should I use attributes?

c# - 使用 AutoFac 和 AutoMock 模拟 CloudBlobClient

c# - Visual Studio 2010 - 卸载 NuGet

c# - "Incorrect number of parameters supplied for lambda declaration"

java - 将图标放置在 AlertDialog 构建器中标题的右侧

xml - 如何将 XML 数据转换为 data.frame?

c# - 使用 linq 调用方法 x 次