反序列化的XML文件中的注释 [英] Deserializing the comments in XML file

查看:193
本文介绍了反序列化的XML文件中的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想反序列化下面的示例XML file.I已创建的模式此XML file.With模式的帮助下,我能够将XML反序列化到对象。

I am trying to deserialize the following sample XML file.I have created the schema for this XML file.With the help of schema i am able to deserialize the XML into object.

但我的问题是我有一个XML注释(例如:<!code>&LT; ----测试 - &GT; )对我的XML file.Deserializer不读书从XML的意见反对我创建使用的模式。

But my problem is i have a XML comments(ex:<!----Test-->) on my XML file.Deserializer is not reading the comments from the XML to object which i created using schema.

同时,我注意到没有为注释节点的架构没有可用的条目。

And also i noted there is no entry available in schema for the comment node.

我怎样才能读取XML文件中的注释对象?

How can i read the comments from XML file to object?

推荐答案

对象序列化的一点是要保存对象的状态,并在以后恢复它。对象字段映射到XML元素和属性,反之亦然。 XMLSerializer的不映射任何意见或反之亦然,所以你不能反序列化的意见的任何东西在你的对象。

The point of object serialization is to save the state of an object, and restore it later. Object fields are mapped to XML elements and attributes, and vice versa. XMLSerializer does not map anything to comments or vice versa, so you can't deserialize comments to anything in your object.

然而,如果你使用一个XmlReader(如@Amigable说),你传递给的反序列化()方法,你就可以使用的的XmlReader分别穿越寻求意见树。

However if you use an XmlReader (as @Amigable said) which you pass to the Deserialize() method, you can then use that XmlReader to separately traverse the tree looking for comments.

不幸的是这使得它更难连接意见反序列化的成员,但也许你可以使用反序列化的节点事件处理程序,以帮助这一点。

Unfortunately this makes it harder to connect the comments to the deserialized members, but maybe you could use deserialization node event handlers to help with that.

更新:一点点阐述使用一个XmlReader与反序列化:

Update: a little elaboration on using an XmlReader with Deserialize:

您列出您的code为:

You listed your code as:

XmlSerializer objSer = new XmlSerializer(typeof(CustomSchema));
StreamReader srmRdr = new StreamReader("Test.XML");
objForm = (CustomSchema)objSer.Deserialize(srmRdr);

我不知道.NETCF或WM任何东西。 (我不知道XmlSerializer的东西要么但我只是在看的的文档)。但是这里是我试图描述上面。

I don't know anything about .NETCF or WM. (I didn't know anything about XmlSerializer either but I'm just looking at the docs.) However here's what I was trying to describe above.

我想你可以使用一个XmlReader进行反序列化(),然后重新使用它,但显然它是只进,因此不能被重置为开头。所以,当您的反序列化,重新打开的test.xml与一个XmlReader:

I thought you could use an XmlReader for Deserialize() and then re-use it, but apparently it's forward-only and therefore can't be reset to the beginning. So After your deserialization, re-open "Test.XML" with an XmlReader:

XmlReader xmlRdr = XmlReader.Create("Test.XML");

然后使用解析 code所示

    // Parse the file
    while (xmlRdr.Read())
    {
        switch (xmlRdr.NodeType)
        {
            case XmlNodeType.Element:
                // You may need to capture the last element to provide a context
                // for any comments you come across... so copy xmlRdr.Name, etc.
                break;
            case XmlNodeType.Comment:
                // Do something with xmlRdr.value

这篇关于反序列化的XML文件中的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆