将类列表序列化为XML

时间:2008-10-07 15:07:00

标签: c# xml serialization

我有一系列要将其序列化为XML文件的类。它看起来像这样:

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

条形图只是属性集合的包装器,如下所示:

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

我希望将其标记为输出到XML文件 - 这将用于持久性,并且还可以通过XSLT将设置呈现为一个很好的人类可读形式。

我希望得到一个很好的XML表示:

<?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中的所有条形图都写出了所有属性。我很确定我会在类定义上需要一些标记才能使它工作,但我似乎找不到合适的组合。

我用属性

标记了Foo
[XmlRoot("Foo")]  

list<Bar>属性

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

试图告诉Serializer我想要发生什么。这似乎不起作用,我只是得到一个空标记,看起来像这样:

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

我不确定我使用自动属性的事实是否会产生任何影响,或者如果使用泛型需要任何特殊处理。我已经使用了更简单的类型,比如字符串列表,但到目前为止,我还没有列出类。

4 个答案:

答案 0 :(得分:31)

要检查一下,您是否将Bar标记为[可序列化]?

此外,您需要Bar上的无参数ctor来反序列化

嗯,我用过:

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>

答案 1 :(得分:4)

一切看起来都很棒。正如@Carl所说,您需要将 [Serializable] 属性添加到您的类中,但除此之外,您的XML创建应该可以找到。

<强>富

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

<强>酒吧

[Serializable]
public class Bar
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

要测试的代码

Foo f = new Foo();
f.BarList = new List<Bar>();
f.BarList.Add(new Bar() { Property1 = "s", Property2 = "2" });
f.BarList.Add(new Bar() { Property1 = "s", Property2 = "2" });

FileStream fs = new FileStream("c:\\test.xml", FileMode.OpenOrCreate);
System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(typeof(Foo));
s.Serialize(fs, f);

<强>输出

<?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>s</Property1> 
            <Property2>2</Property2> 
        </Bar>
        <Bar>
            <Property1>s</Property1> 
            <Property2>2</Property2> 
        </Bar>
    </BarList>
</Foo>

答案 2 :(得分:1)

此项目发布至今已超过5年。我从2013年7月开始提供我的经验(.NET Framework 4.5)。对于它的价值和它可能涉及的人:

当我定义一个类这样的类:(VB.Net code)

<Serializable> Public Class MyClass
    Public Property Children as List(of ChildCLass)
    <XmlAttribute> Public Property MyFirstProperty as string
    <XmlAttribute> Public Property MySecondProperty as string
End Class

<Serializable> Public Class ChildClass
    <XmlAttribute> Public Property MyFirstProperty as string
    <XmlAttribute> Public Property MySecondProperty as string
End Class

使用此定义,类(de)序列化没有任何问题。这是来自这里的XML:

<MyClass> MyFirstProperty="" MySecondProperty=""
    <Children>
        <ChildClass> MyFirstProperty="" MySecondProperty=""
        </ChildClass>
   </Children>
</MyClass>

我花了两天的时间才弄清楚解决方案是省略List(of T)元素的<XmlElement>前缀。

答案 3 :(得分:-1)

var xmlfromLINQ = new XElement("BarList",
            from c in BarList 
            select new XElement("Bar",
                new XElement("Property1", c.Property1),
                new XElement("Property2", c.Property2)
             ));