XmlWriter.WriteRaw 缩进
我有一个类似的问题,例如 注入 XML 时的 XML 缩进将字符串写入 XmlWriter。
我还使用 XmlWriter.WriteRaw 将 XML 片段输出到 xml 节点中。该文档片段不遵循标识,这很好。上面的帖子给出的答案不适用于 XML 片段。好吧,我可以使用 XmlReader
读取片段,但是 WriteNode
不适用于文档片段(请参阅 http://msdn.microsoft.com/en-us/library/1wd6aw1b.aspx)。
关于如何作弊以获得正确缩进的任何提示?
感谢您的任何提示
I have a similar question like XML indenting when injecting an XML string into an XmlWriter.
I also use XmlWriter.WriteRaw
to output an XML fragment into an xml node. This document fragment does not follow the idention which would be nice. The answer given on the post above does not work for XML fragments. Well, I can read the fragment with the XmlReader
, but WriteNode
does not work with document fragments (see http://msdn.microsoft.com/en-us/library/1wd6aw1b.aspx).
Any hints on how to cheat to get proper indentation?
Thanks for any hints
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个老问题,但我今天遇到了同样的问题。我的解决方案是使用 XmlDocument.WriteContentTo 方法:
https://dotnetfiddle.net/a2928r
It's an old question, but I got the same problem today. My solution is to use XmlDocument.WriteContentTo method:
https://dotnetfiddle.net/a2928r
您可以在内存中构建一个包含 xml 片段的有效 Xml 文档。然后使用
XmlReader
读取此文档(例如使用MemoryStream
),并让XmlWriter
写入缩进的xml。更快的方法是通过操作字符串来缩进 xml。搜索
<
,增加嵌套级别并添加缩进空格。如果您发现或自闭标签降低嵌套级别并附加
\n
我认为没有快速的和 很好地解决了你的问题,但我可能是错的......
You could build a valid Xml-Document in memory containing your xml fragment. Then read this document with the
XmlReader
(e.g. by usingMemoryStream
) and let theXmlWriter
write indented xml.A faster approach wold be indenting the xml yourself by manipulating the string. Search for
<
, increase the nesting level and add the indention spaces. If you find</
or a self closing tag decrease the nesting level and append a\n
I don't think there is a fast and nice solution to your problem, but i might be wrong...