将类列表序列化为 XML [英] Serializing Lists of Classes to XML

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

问题描述

我有一组要序列化为 XML 文件的类.它看起来像这样:

I have a collection of classes that I want to serialize out to an XML file. It looks something like this:

public class Foo
{
  public List<Bar> BarList { get; set; }
}

其中 bar 只是一组属性的包装器,如下所示:

Where a bar is just a wrapper for a collection of properties, like this:

public class Bar
{
  public string Property1 { get; set; }
  public string Property2 { get; set; }
}

我想标记它以便它输出到一个 XML 文件 - 这将用于持久性,并通过 XSLT 将设置呈现为一个很好的人类可读的形式.

I want to mark this up so that it outputs to an XML file - this will be used for both persistence, and also to render the settings via an XSLT to a nice human-readable form.

我想得到这样一个很好的 XML 表示:

I want to get a nice XML representation like this:

<?xml version="1.0" encoding="utf-8"?>
<Foo>
  <BarList>
    <Bar>
      <Property1>Value</Property1>
      <Property2>Value</Property2>   
    </Bar>
    <Bar>
      <Property1>Value</Property1>
      <Property2>Value</Property2>   
    </Bar>
  </Barlist>
</Foo>

Barlist 中的所有 Bars 在哪里都写出了它们的所有属性.我相当确定我需要在类定义上进行一些标记才能使其工作,但我似乎找不到正确的组合.

where are all of the Bars in the Barlist are written out with all of their properties. I'm fairly sure that I'll need some markup on the class definition to make it work, but I can't seem to find the right combination.

我已经用属性标记了 Foo

I've marked Foo with the attribute

[XmlRoot("Foo")]  

和带有属性的list

[XmlArray("BarList"), XmlArrayItem(typeof(Bar), ElementName="Bar")]

试图告诉序列化程序我想要发生什么.然而,这似乎不起作用,我只是得到一个空标签,如下所示:

in an attempt to tell the Serializer what I want to happen. This doesn't seem to work however and I just get an empty tag, looking like this:

<?xml version="1.0" encoding="utf-8"?>
<Foo> 
  <Barlist />
</Foo>

我不确定我使用自动属性的事实是否应该有任何影响,或者泛型的使用是否需要任何特殊处理.我已经使用它来处理更简单的类型,例如字符串列表,但到目前为止我还没有使用类列表.

I'm not sure if the fact I'm using Automatic Properties should have any effect, or if the use of generics requires any special treatment. I've gotten this to work with simpler types like a list of strings, but a list of classes so far eludes me.

推荐答案

只是想检查一下,您是否将 Bar 标记为 [Serializable]?

Just to check, have you marked Bar as [Serializable]?

另外,你需要一个 Bar 上的无参数构造函数来反序列化

Also, you need a parameter-less ctor on Bar, to deserialize

嗯,我用过:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        Foo f = new Foo();

        f.BarList = new List<Bar>();

        f.BarList.Add(new Bar { Property1 = "abc", Property2 = "def" });

        XmlSerializer ser = new XmlSerializer(typeof(Foo));

        using (FileStream fs = new FileStream(@"c:sertest.xml", FileMode.Create))
        {
            ser.Serialize(fs, f);
        }
    }
}

public class Foo
{
    [XmlArray("BarList"), XmlArrayItem(typeof(Bar), ElementName = "Bar")]
    public List<Bar> BarList { get; set; }
}

[XmlRoot("Foo")]
public class Bar
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

然后产生:

<?xml version="1.0"?>
<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <BarList>
    <Bar>
      <Property1>abc</Property1>
      <Property2>def</Property2>
    </Bar>
  </BarList>
</Foo>

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

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