反序列化 XML 文件中的注释
我正在尝试反序列化以下示例 XML 文件。我已经为此 XML 文件创建了架构。借助架构,我能够将 XML 反序列化为对象。
但我的问题是我的 XML 文件上有 XML 注释(例如:)。反序列化器没有从 XML 读取注释到我的对象使用模式创建。
我还注意到模式中没有可用于评论节点的条目。
如何从 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.
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.
How can i read the comments from XML file to object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对象序列化的要点是保存对象的状态,并在以后恢复它。对象字段映射到 XML 元素和属性,反之亦然。 XMLSerializer 不会将任何内容映射到注释,反之亦然,因此您无法将注释反序列化到对象中的任何内容。
但是,如果您使用 XmlReader (如 @Amigable 所说),您将其传递给 反序列化< /a>() 方法,然后您可以使用该 XmlReader单独遍历树寻找评论。
不幸的是,这使得将注释连接到反序列化成员变得更加困难,但也许您可以使用反序列化节点事件处理程序来帮助解决这个问题。
更新:有关使用 XmlReader 与反序列化的一些详细说明:
您将代码列出为:
我对 .NETCF 或 WM 一无所知。 (我对 XmlSerializer 也一无所知,但我只是在查看 文档。)但这就是我上面试图描述的内容。
我认为您可以使用 XmlReader 进行 Deserialize(),然后重新使用它,但显然它是仅向前的,因此无法重置为开头。因此,反序列化后,使用 XmlReader 重新打开“Test.XML”:
然后使用解析 此处显示的代码:
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.
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.
Update: a little elaboration on using an XmlReader with Deserialize:
You listed your code as:
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.
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:
Then use the parsing code shown here:
它没有说明您正在使用哪种编程语言,而是基于 此示例,这与你想要做的完全相反,
您不能像插入 XmlWriter 作为该问题的可接受答案一样插入 XmlReader 吗?
It does not say which programming language you are using, but based on this example, which is the exact opposite of what you are trying to do,
could you not insert an XmlReader like how an XmlWriter was inserted as accepted answer to that question?