如何拥有像 这样的特殊实体	使用 LinqXml 时在 xml 文档输出中
我正在尝试使用 LinqXml 生成 xml 文档,其中“\n”为“& #10;”在 XElement 值中,无论我尝试使用 XmlWriter 进行什么设置,它仍然会在最终的 xml 文档中发出“\n”。
我确实尝试了以下方法,扩展了 XmlWriter。 重写 XmlWriterSettings 更改了换行处理。 这两种选择都不适合我。
任何帮助/指示将被采纳。
问候 斯蒂芬
I am trying to generate a xml document using LinqXml, which has the "\n" to be "& #10;" in the XElement value, no matter whatever settings I try with the XmlWriter, it still emits a "\n" in the final xml document.
I did try the following, Extended the XmlWriter.
Overrided the XmlWriterSettings changed the NewLine Handling.
Both of the options didnt work out for me.
Any help/pointers will be appriciated.
Regards
Stephen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LINQ to XML 在 XmlReader/XmlWriter 之上工作。 XmlReader 是 XML 处理器/解析器的实现,如 XML 规范中所述。该规范基本上表明解析器需要从上面的应用程序中隐藏文本中的实际表示。这意味着 \n 和
应该报告为同一件事。这就是它的作用。
XmlWriter 向后也是同样的事情。它的目的是以这样的方式保存输入,这样在解析时你会得到完全相同的东西。
因此,写入文本值“\n”将这样写入,以便解析器将报告回“\n”(在这种情况下,文本节点的输出文本为 \n,但是
对于由于属性值中发生的标准化而导致的属性)。
按照这个想法尝试写一个文本值“
”实际上会写出“ ”,因为当读者解析时,它将返回原始的“
”。LINQ
to XML 使用 XmlWriter 将树保存到 XML 文件中。因此您将得到上述行为。
您可以自己将树(或其中的一部分)写入 XmlWriter,在这种情况下您可以获得更多控制权。特别是它将允许您使用 XmlWriter.WriteCharEntity 方法,该方法强制编写器将指定字符输出为字符实体,即 $#xXX; 格式(请注意,它将使用十六进制格式,而不是十进制)。
LINQ to XML works on top of XmlReader/XmlWriter. The XmlReader is an implementation of the XML processor/parser as described in the XML spec. That spec basically says that the parser needs to hide the actual representation in the text from the application above. Meaning that both \n and should be reported as the same thing. That's what it does.
XmlWriter is the same thing backwards. It's purpose is to save the input in such a way, that when parsed you will get exactly the same thing back.
So writing a text value "\n" will write it such that the parser will report back "\n" (in this case the output text is \n for text node, but for attribute due to normalization which occurs in attribute values).
Following that idea trying to write a text value " " will actually write out " " because when the reader parses that it will get back the original " ".
LINQ to XML uses XmlWriter to save the tree to an XML file. So you will get the above behavior.
You could write the tree into the XmlWriter yourself (or part of it) in which case you get more control. In particular it will allow you to use the XmlWriter.WriteCharEntity method which forces the writer to output the specified character as a character entity, that is in the $#xXX; format. (Note that it will use the hex format, not the decimal).
XML 元素中存在“\n”转义值的原因是什么?换行符在 XML 元素内有效,当您再次解析 XML 时,它将按照您的预期进行解析。
如果将换行符放置在 XML 属性的值中,就会发生您正在寻找的情况:
输出:
What is the reason for having the escaped value for '\n' in the XML element? The newline character is valid inside an XML element and when you parse the XML again, it will be parsed as you expect.
What you're looking for would happen if the newline character is placed within the value of an XML attribute:
Output: