简体   繁体   English

是否可以在同一xml文件中序列化来自不同类的对象?

[英]Is it possible to serialize objects made from different class in the same xml file?

For example, I got the class Header , and the class Dog . 例如,我有Header类和Dog类。 I would like a file that looks like this: 我想要一个看起来像这样的文件:

在此处输入图片说明

So far, I have seen no examples to perform this. 到目前为止,我还没有看到执行此操作的示例。 I must use the serializer.Serialize because I got a collection of thousands of objects. 我必须使用serializer.Serialize因为我有成千上万个对象的集合。 Doing otherwise would take too long. 否则将花费太长时间。

If it is impossible, I will make two different files, one with the header and the other with the collection of objects, but it is not an interesting solution. 如果不可能,我将制作两个不同的文件,一个带有标头,另一个带有对象集合,但这不是一个有趣的解决方案。

Edit: 编辑:
I can't use a wrapper since I don't have my complete dog list at the beginning. 我不能使用包装器,因为开始时没有完整的狗列表。 I receive one at the time until I stop the application 我一次收到一份,直到我停止申请

Thanks a lot! 非常感谢!

You will need to create another class the can represent this. 您将需要创建另一个可以表示该类的类。 For example. 例如。

public class Kennel
{
    public Header Header { get; set; }
    public List<Dog> Dogs { get; set; }
}

This can then be serialized/deserialized easily. 然后可以轻松地对其进行序列化/反序列化。

In answer to your question. 在回答您的问题。 If you need the XML to be identical to yours, you will need to create a custom wrapper that will read and write the XML into that specific format. 如果您需要与自己的XML相同的XML,则需要创建一个自定义包装器,以将XML读取和写入该特定格式。

I would looking at making a custom wrapper class (call it: DogWrapper or something) that contained a Header object and a list (or array, or whatever) of type Dog . 我将研究制作一个自定义包装类(称为: DogWrapper或其他类), DogWrapper包含一个Header对象和一个Dog类型的列表(或数组,或任何其他形式)。 Then use custom serialization to define how to write out that object. 然后使用自定义序列化定义如何写出该对象。

Then you just marshal your header and dogs into a DogWrapper, and serialize the DogWrapper. 然后,您只需将标头和狗编组到DogWrapper中,并序列化DogWrapper。

暂无
暂无

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

相关问题 如何将不同的对象序列化为同一个XML文件然后反序列化 - How to serialize different objects to the same XML file and then deserialize 在XML文件中对对象进行序列化/反序列化的问题 - Problem with Serialize/Deserialize objects to/from XML file 如何序列化/反序列化从公共类继承并遵循相同接口的对象列表? - How is it possible to serialize/deserialize a list of objects that inherit from a common class and adhere to the same Interface? 使用相同的类反序列化不同的可能对象 - Deserializing different possible objects using the same class 将XML序列化为具有不同名称的类 - Serialize XML to a class with a different name 是否有可能对返回字符串对象列表的属性(在xml序列化类中)进行xml序列化? - Is it possible to xml-serialize a property (in a xml serialized class) that returns a list of string objects? 如何将类对象序列化为xml文档 - how to serialize class objects to xml document 如何反序列化包含具有相同父类的不同类类型的对象的 JSON 数组 - How to de-serialize JSON array containing objects of different class types that have same parent class 是否可以从XML文件创建多个对象? - Is it possible to create multiple objects from an XML file? 如何在C#中将SAme类的多个对象序列化为单个文件并反序列化为这些对象 - How to Serialize Multiple Objects of the SAme Class into a single file and Deseialze into Those objects in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM