维护XML的层次结构(即父母与子女)由XmlSerializer的生成中的对象的信息子女、层次、对象、父母

2023-09-06 07:44:29 作者:可能是因為^帥

一段时间以来我一直在努力解决以下问题,我开始江郎才尽:

for some time now I have been trying to solve the following problem and I'm starting to run out of ideas:

我已经生成的XSD架构使用xsd.exe工具和反序列化XML文件正常工作了一组C#类的。问题是,除了便利性和使用自动生成的类的安全,我还需要对XML层次结构的信息,即我需要建立反序列化过程中创建的对象之间的父子关系。请注意,我想避免保持一个单独的XML层次结构(比如DOM树),而是使生成的对象跟踪他们的父母和孩子。

I have generated a set of C# classes from an xsd schema using the xsd.exe tool and deserializing xml files works fine. The problem is that apart from the convenience and safety of using the auto generated classes, I also need information about the xml hierarchy, ie I need to establish parent-child relationships between the objects created during deserialization. Note that I want to avoid keeping a separate xml hierarchy structure (like a DOM tree), but rather make the generated objects keep track of their parents and children.

我已成功地使用JAXB在Java的顺利完成这件事:

I have managed to pull this off in java using JAXB by:

定义一个公共基类的所有反序列化对象。这个基类包含孩子的列表,并引用了一个父对象(如果有的话)。

Defining a common base class for all deserialized objects. This base class contains a list of children and a reference to a parent object (if any).

使用的Unmarshaller.Listener功能,提供了完成反序列化对象的回调。这个回调提供了一个参考最近反序列化对象,这使得建立父子关系平凡的父母。

Using the Unmarshaller.Listener functionality that provides a callback on completed object deserialization. This callback provides a reference to the parent of the recently deserialized object, which makes establishing parent-child relationships trivial.

我会如何在C#这样做呢?我有一个看MSDN文档,做了不少谷歌上搜索,但一直没能找到任何有用的信息。

How would I go about doing this in C#? I have had a look at the MSDN docs and done quite a lot of googling, but haven't been able to find any useful information.

推荐答案

我前段时间写了一篇关于这个确切的问题,也许它可以帮助你。

I wrote an article some time ago about this exact problem, perhaps it can help you.

的http://www.thomaslevesque.com/2009/06/12/c-parentchild-relationship-and-xml-serialization/

 
精彩推荐
图片推荐