XmlWriter.WriteRaw缩进 [英] XmlWriter.WriteRaw indentation
问题描述
我也使用 XmlWriter.WriteRaw
输出XML片段到一个XML节点。本文档片段不遵循这将是很好的idention。对后给出的答案上述不适用于XML片段工作。好吧,我可以读的片段的XmlReader
,但 WriteNode
不与文档片段工作(见的 http://msdn.microsoft.com/en-us/library/1wd6aw1b.aspx )。
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?
感谢
推荐答案
您可以在包含XML片段记忆建立一个有效的XML的文档。然后阅读本文件的的XmlReader
(例如,通过使用的MemoryStream
),并让的XmlWriter
写缩进XML。
You could build a valid Xml-Document in memory containing your xml fragment. Then read this document with the XmlReader
(e.g. by using MemoryStream
) and let the XmlWriter
write indented xml.
一个更快的方法沃尔德是通过操纵字符串自己缩进的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...
这篇关于XmlWriter.WriteRaw缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!