简体   繁体   English

XML序列化C#中多个对象的方法

[英]Ways to XML serialize multiple objects in C#

What are different ways in which we can XML serialize multiple objects of same/different types? XML可以序列化相同/不同类型的多个对象的不同方式有哪些?

For example, Generic List<T> for same type of objects and ArrayList for different type of objects. 例如,Generic List<T>用于相同类型的对象,而ArrayList用于不同类型的对象。

Also, which type of structures are XML serializable and which are not? 另外,哪种类型的结构可以XML序列化,哪些不能序列化?

It would be helpful if anyone can provide with sample piece of code or a link. 如果任何人都可以提供示例代码或链接,这将很有帮助。

To be serializable via XmlSerializer , your types must 要通过XmlSerializer进行序列化,您的类型必须

  • be public 公开
  • have a public parameterless constructor 有一个公共的无参数构造函数
  • it serializes the public read/write fields and properties (properties being preferred) 它序列化公共读/写字段和属性(首选属性)

In terms of which constructs; 关于哪种构造;

  • individual concrete entities are fine 各个具体实体都可以
  • concrete lists/arrays ( List<T> , T[] , etc) are handled fine 具体的列表/数组( List<T>T[]等)可以很好地处理
  • but it will struggle with interfaces, dictionaries, and various generics scenarios (other than lists) 但它会在接口,词典和各种泛型方案(列表除外)中挣扎

You are right that List<T> is fine for serializing a set of like objects. 您很正确, List<T>适合序列化一组相似的对象。 If you are dealing with subclasses, then [XmlInclude] can be used to distinguish types; 如果要处理子类,则可以使用[XmlInclude]来区分类型。 ie if you have: 即如果您有:

var list = new List<ParentType>();
list.Add(new ParentType());
list.Add(new ChildType()); // Child : Parent

then this may still be serializable as long as you have: 那么只要您具备以下条件,它就仍可以序列化:

[XmlInclude(typeof(Child))]
public class Parent {}

public class Child : Parent {}

(you can also specify this relationship in the ctor to XmlSerializer , but it is extra work) (您也可以在ctor中指定此关系到XmlSerializer ,但这是额外的工作)

I don't recommend using IXmlSerializable unless you have to; 我不建议您使用IXmlSerializable除非您必须这样做。 it is complex and hard to get right. 这很复杂,很难解决。 More importantly; 更重要的是; it doesn't work **at all* if you are using wsdl-generated proxy objects over a SOAP service; 如果您正在通过SOAP服务使用wsdl生成的代理对象,那么“根本不起作用”。 the other end won't have your IXmlSerializable implementation. 另一端将没有您的IXmlSerializable实现。

I also don't recommend serializing lists of arbitrary object types; 我也不建议序列化任意对象类型的列表。 if you have a finite set of known types there are tricks to do this utilising [XmlInclude] etc. 如果您有一组有限的已知类型,则可以使用[XmlInclude]等技巧来实现。

You could make all your classes implement System.Xml.Serialization.IXmlSerializable. 您可以使所有类都实现System.Xml.Serialization.IXmlSerializable。 You could then have a generic collection of this type. 然后,您可以拥有这种类型的通用集合。 It is then very simple to implement the ReadXml and WriteXml methods required. 这样,实现所需的ReadXml和WriteXml方法非常简单。

eg 例如

class MyClass : IXmlSerializable
{
        public System.Xml.Schema.XmlSchema GetSchema()
        {            
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            //TODO: read logic here...
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            //TODO: write logic here...
        }
}

Usage... 用法...

class WorkerClass
{
  public void SerializeListToFile(IList<IXmlSerializable> list, string fileName)
  {
    using (XmlWriter writer = new XmlTextWriter(fileName))
    {
      foreach (IXmlSerializable item in list)
        item.WriteXml(writer);

      writer.Close();
    }
  }
}

Here is a link for Xml serialization of collections . 这是集合的Xml序列化的链接。 But Chris Arnold is right implementing IXmlSerializable will allow you to have full control on the serialization. 但是克里斯·阿诺德(Chris Arnold)正确地实现IXmlSerializable将使您可以完全控制序列化。 Xml attributes are attractive because they are declarative but the other option is worth learning (and is faster from my experience). Xml属性之所以具有吸引力是因为它们是声明性的,但是另一个选择值得学习(根据我的经验,它更快)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM