如何将 EntityReference(例如  )写出到 XmlWriter,由 XmlNodeReader 读取?

发布于 2024-09-29 15:32:45 字数 1372 浏览 3 评论 0原文

背景
我正在使用读取器和写入器读取和写入 XML 文档,并在读取和写入操作之间使用过滤逻辑来确定应该写回哪些读取的部分(实际上它会删除一些标签)...

我当前选择的实现使用以下内容:XmlNodeReaderXmlWriterXmlNodeType

using System.Xml;

// Read every node
using (XmlNodeReader reader = new XmlNodeReader(xmlFragment)) {

    // But write out only nodes I want
    using (XmlWriter writer = XmlTextWriter.Create(sb, writerSettings)) {

        while (reader.Read()) {

            //... with filtering logic here to choose what is to be written.

            //  I do want to write out all entity references
            if (XmlNodeType.EntityReference == reader.NodeType)
                ; // but how do I do that here?
        }
    }
}

问题
当我遇到阅读器的实体引用时,我在 reader.Value 中得到一个空字符串“”,但它实际上是 XML 源中的一个   值,已被遭遇。所以它被检测到了,但我无法拥有。

显然是一种特殊情况或边缘情况:
我发现以下 MSDN 文章表明实体引用有些特殊在解析时,但我无法将其用于工作解决方案。细节似乎很少——我希望有一个代码示例来补充它。

Background
I'm reading and writing an XML document using reader and writer, with filtering logic in between the read and write operations to determine which parts read should be written back out (effectively it strips some tags out) ...

My choice of implementation currently is the following using: XmlNodeReader, XmlWriter and XmlNodeType

using System.Xml;

// Read every node
using (XmlNodeReader reader = new XmlNodeReader(xmlFragment)) {

    // But write out only nodes I want
    using (XmlWriter writer = XmlTextWriter.Create(sb, writerSettings)) {

        while (reader.Read()) {

            //... with filtering logic here to choose what is to be written.

            //  I do want to write out all entity references
            if (XmlNodeType.EntityReference == reader.NodeType)
                ; // but how do I do that here?
        }
    }
}

Problem
When I encounter an entity reference with the reader I get an empty string "" in reader.Value however it's really an   value in the XML source that has been encountered. So it's detected, but not available for me to have.

Apparently a Speciality or Edge Case:
I found the following MSDN article that indicates entity references are somewhat special while parsing, but I have been unable to use it toward a working solution. It seems sparse on details - I would prefer there be a code sample to supplement it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

郁金香雨 2024-10-06 15:32:45

您可以参考此链接: http://msdn.microsoft .com/en-us/library/system.xml.xmlreader.name.aspx

希望这有帮助!

XmlReader.Name 属性
当在派生类中重写时,获取
当前的限定名称
节点.节点。

You may refer this link: http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.name.aspx

Hope this helps!!

XmlReader.Name Property
When overridden in a derived class, gets
the qualified name of the current
node.node.

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