XML序列化未正确导出列表

时间:2015-10-23 12:53:38

标签: c# xml serialization xml-parsing

我正在尝试将对象序列化为XML,该XML应该生成如下的XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<XMLFile xmlns="http://www.google.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <channels>
  <Ch01>
    <Name>Test Channel 01</Name>
    <Number>1</Number>
  </Ch01>
  <Ch02>
    <Company>Google</Company>
    <Founded>2015-10-23T13:04:04.2048888+01:00</Founded>
    <Founder>Some Guy</Founder>
  </Ch02>
  </channels>
</XMLFile>

不幸的是,当我进行序列化时,标签丢失了,我花了两个小时试图解决它,我有一个同事的样子,他们也很难过,下面是代码的复制品(不是我们真正的东西)项目)它有同样的问题。

测试代码:

        XMLFile file = new XMLFile();
        file.channels.Add(new Ch01() {Name = "Test Channel 01", Number = 1});
        file.channels.Add(new Ch02() {Company = "Google", Founded = DateTime.Now, Founder = "Some Guy"});
        XMLSerialize.SerializeToXml(Application.StartupPath + "//test.xml", file);

XMLFILE:

[Serializable]
public class XMLFile
{
    public XMLFile()
    {

    }

    [XmlElement(Type = typeof(Ch01))]
    [XmlElement(Type = typeof(Ch02))]
    public List<channel> channels = new List<channel>(); 
}

信道:

[Serializable]
public class channel
{
    public channel()
    {

    }
}

CH01:

[Serializable]
public class Ch01 : channel
{
    public Ch01()
    {

    }

    public string Name;
    public int Number;
}

CH02:

[Serializable]
public class Ch02 : channel
{
    public Ch02()
    {

    }

    public string Company;
    public DateTime Founded;
    public string Founder;
}

XMLSerialize来:

public static class XMLSerialize
{
    public static void SerializeToXml<T>(string file, T value)
    {
        var serializer = new XmlSerializer(typeof(T), "http://www.google.com");
        using (var writer = XmlWriter.Create(file))
            serializer.Serialize(writer, value);
    }

    public static T DeserializeFromXML<T>(string file)
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(T), "http://www.google.com");
        TextReader textReader = new StreamReader(file);
        T result;
        result = (T)deserializer.Deserialize(textReader);
        textReader.Close();

        return result;
    }

以下是我得到的输出:

<?xml version="1.0" encoding="UTF-8"?>
<XMLFile xmlns="http://www.google.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Ch01>
      <Name>Test Channel 01</Name>
      <Number>1</Number>
   </Ch01>
   <Ch02>
      <Company>Google</Company>
      <Founded>2015-10-23T13:04:04.2048888+01:00</Founded>
      <Founder>Some Guy</Founder>
   </Ch02>
</XMLFile>

使用它的项目将生成一个巨大的XML文件,因此通过XmlDocument手动创建它不是一个好的选择。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

根据评论中的建议,您可以使用XmlArray,但您需要指定派生类的类型以及如下所示:

public class XMLFile
{
    public XMLFile()
    {

    }

    [XmlArray("channels")]
    [XmlArrayItem(Type = typeof(Ch01), ElementName = "Ch01")]
    [XmlArrayItem(Type = typeof(Ch02), ElementName = "Ch02")]
    public List<channel> channels = new List<channel>();
}