[英]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.