如何将 EntityReference(例如 )写出到 XmlWriter,由 XmlNodeReader 读取?
背景
我正在使用读取器和写入器读取和写入 XML 文档,并在读取和写入操作之间使用过滤逻辑来确定应该写回哪些读取的部分(实际上它会删除一些标签)...
我当前选择的实现使用以下内容:XmlNodeReader,XmlWriter 和 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?
}
}
}
问题
当我遇到阅读器的实体引用时,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以参考此链接: http://msdn.microsoft .com/en-us/library/system.xml.xmlreader.name.aspx
希望这有帮助!
You may refer this link: http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.name.aspx
Hope this helps!!