XML序列化未正确导出列表(XML Serialization not exporting list correctly)

编程入门 行业动态 更新时间:2024-10-10 01:19:52
XML序列化未正确导出列表(XML Serialization not exporting list correctly)

我正在尝试将对象序列化为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手动创建它不是一个好的选择。

有任何想法吗?

I am trying to serialize an object to XML that should generate an XML document like this:

<?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>

Unfortunately the tag is missing when I do the serialization, I have spent two hours trying to figure it out and I have had a colleague look and they also are stumped, below is a replica the code(not the real stuff from our project) it has the same issue.

Test code:

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>(); }

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

Here is the output I get:

<?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>

The project this is used in will generate a huge XML file so creating it manually via XmlDocument is not a good option.

Any ideas?

最满意答案

正如评论中建议的那样,您可以使用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>(); }

As suggested in the comments you can use XmlArray but you need to specify the types of derived classes as well as shown below:

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>(); }

更多推荐

本文发布于:2023-07-30 15:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338822.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   序列化   列表   XML   correctly

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!