反序列化 XML 文件中的注释

发布于 2024-09-26 03:23:37 字数 219 浏览 4 评论 0原文

我正在尝试反序列化以下示例 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉絮湮 2024-10-03 03:23:37

对象序列化的要点是保存对象的状态,并在以后恢复它。对象字段映射到 XML 元素和属性,反之亦然。 XMLSerializer 不会将任何内容映射到注释,反之亦然,因此您无法将注释反序列化到对象中的任何内容。

但是,如果您使用 XmlReader (如 @Amigable 所说),您将其传递给 反序列化< /a>() 方法,然后您可以使用该 XmlReader单独遍历树寻找评论。

不幸的是,这使得将注释连接到反序列化成员变得更加困难,但也许您可以使用反序列化节点事件处理程序来帮助解决这个问题。

更新:有关使用 XmlReader 与反序列化的一些详细说明:

您将代码列出为:

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

我对 .NETCF 或 WM 一无所知。 (我对 XmlSerializer 也一无所知,但我只是在查看 文档。)但这就是我上面试图描述的内容。

我认为您可以使用 XmlReader 进行 Deserialize(),然后重新使用它,但显然它是仅向前的,因此无法重置为开头。因此,反序列化后,使用 XmlReader 重新打开“Test.XML”:

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

然后使用解析 此处显示的代码

    // 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

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:

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

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:

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

Then use the parsing code shown here:

    // 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
A君 2024-10-03 03:23:37

它没有说明您正在使用哪种编程语言,而是基于 此示例,这与你想要做的完全相反,
您不能像插入 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文