我正在尝试将对象序列化为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>(); }更多推荐
发布评论