【问题标题】:XML Serialization not exporting list correctlyXML 序列化未正确导出列表
【发布时间】:2015-10-23 12:53:37
【问题描述】:

我正在尝试将一个对象序列化为 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);

XML 文件:

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

XML序列化:

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 手动创建它不是一个好的选择。

有什么想法吗?

【问题讨论】:

  • 当您说“标签丢失”时,您指的是顶部的 XML 声明(“
  • 抱歉忘记了。我已经添加了真实的输出, 标签丢失了
  • 不应该[XmlArray("channels")] 为你做这件事吗?
  • 你会期望 [XmlArray] 工作,但随后抛出错误“有一个错误反映类型”,因为它不再知道如何处理 Ch01/Ch02(即使我没有删除他们)

标签: c# xml serialization xml-parsing


【解决方案1】:

根据 cmets 的建议,您可以使用 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>();
}

【讨论】:

  • 不错。我不知道 [XmlArrayItem] 存在!这似乎有效,所以谢谢你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
相关资源
最近更新 更多