的XmlWriter插入空格当XML:空间=保留 [英] XmlWriter inserting spaces when xml:space=preserve

查看:761
本文介绍了的XmlWriter插入空格当XML:空间=保留的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var doc = new XmlDocument();
doc.LoadXml("<?xml version=\"1.0\"?><root>"
    + "<value xml:space=\"preserve\">"
    + "<item>content</item>"
    + "<item>content</item>"
    + "</value></root>");

var text = new StringWriter();
var settings = new XmlWriterSettings() { Indent = true, CloseOutput = true };
using (var writer = XmlWriter.Create(text, settings))
{
    doc.DocumentElement.WriteTo(writer);
}

var xml = text.GetStringBuilder().ToString();
Assert.AreEqual("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<root>\r\n"
    + "  <value xml:space=\"preserve\"><item>content</item>"
    + "<item>content</item></value>\r\n</root>", xml);



因为的XmlWriter 是插入断言失败换行和缩进周围的<项目> 元素,这似乎违背了 XML:空间=保存属性。

The assertion fails because the XmlWriter is inserting a newline and indent around the <item> elements, which would seem to contradict the xml:space="preserve" attribute.

我试图把输入不带空格(或仅显著的空白,并已加载到的XmlDocument ),并漂亮地打印它的没有的补充内标保留空白(原因很明显)元素任何空白。

I am trying to take input with no whitespace (or only significant whitespace, and already loaded into an XmlDocument) and pretty-print it without adding any whitespace inside elements marked to preserve whitespace (for obvious reasons).

这是一个bug还是我做错了什么? ?有没有更好的方式来实现我想要做的。

Is this a bug or am I doing something wrong? Is there a better way to achieve what I'm trying to do?

修改:我也许应该补充一点,我就必须使用的XmlWriter 缩进= TRUE 在输出端。在真实的代码,这是从我的代码之外传递。

Edit: I should probably add that I do have to use an XmlWriter with Indent=true on the output side. In the "real" code, this is being passed in from outside of my code.

推荐答案

好吧,我已经找到了解决方法。

Ok, I've found a workaround.

原来,的XmlWriter 做正确的事情,如果有真正的 XML中的任何空白:空格=保留块 - 只有当没有任何它的螺丝并增加了一些。方便,这也是工作,如果有一些空白节点,即使它们是空的。所以,我想出来的诀窍是试图将它写出来之前,装饰用在适当的地方额外的长度为0的空白文档。结果正是我想:漂亮的印花无处不在,除了这里的空白也是显著

It turns out that XmlWriter does the correct thing if there actually is any whitespace within the xml:space="preserve" block -- it's only when there isn't any that it screws up and adds some. And conveniently, this also works if there are some whitespace nodes, even if they're empty. So the trick that I've come up with is to decorate the document with extra 0-length whitespace in the appropriate places before trying to write it out. The result is exactly what I want: pretty printing everywhere except where whitespace is significant.

解决方法是内部块更改为:

The workaround is to change the inner block to:

PreserveWhitespace(doc.DocumentElement);
doc.DocumentElement.WriteTo(writer);



...

...

private static void PreserveWhitespace(XmlElement root)
{
    var nsmgr = new XmlNamespaceManager(root.OwnerDocument.NameTable);
    foreach (var element in root.SelectNodes("//*[@xml:space='preserve']", nsmgr)
        .OfType<XmlElement>())
    {
        if (element.HasChildNodes && !(element.FirstChild is XmlSignificantWhitespace))
        {
            var whitespace = element.OwnerDocument.CreateSignificantWhitespace("");
            element.InsertBefore(whitespace, element.FirstChild);
        }
    }
}



我还在想, 的XmlWriter 的这种行为是一个错误,但。

I'm still thinking that this behaviour of XmlWriter is a bug, though.

这篇关于的XmlWriter插入空格当XML:空间=保留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆