的XDocument保存保存标签内的空白 [英] xdocument save preserve white space inside tags

查看:326
本文介绍了的XDocument保存保存标签内的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在LINQ使用的XDocument编辑(插入),并保存XML文档。

I am using XDocument in LINQ to edit (insert) and save xml document.

XDocument doc = XDocument.Load("c:\\sample.xml", LoadOptions.PreserveWhitespace);
doc.Save("c:\\sample.xml",SaveOptions.DisableFormatting)

sample.xml中doc.Save前:

sample.xml before doc.Save :

<ELEMENT ATTRIB1="attrib1"  ATTRIB2="attrib2" >
    value
</ELEMENT>



sample.xml中后doc.Save

sample.xml after doc.Save

<ELEMENT ATTRIB1="attrib1" ATTRIB2="attrib2">
    value
</ELEMENT>



正如你所看到的,有ATTRIB1后双空间和原始文档中ATTRIB2后空单。
,而这些空间已经被去除LINQ当我叫doc.save。

As you can see, there is double space after ATTRIB1 and a single space after ATTRIB2 in the original document. But these spaces have been removed by linq when I call doc.save.

我怎样才能保持内标签的空格?

How can I preserve the whitespaces inside tag?

推荐答案

我相信 LoadOptions.PreserveWhitespace SaveOptions.DisableFormatting 只是指导如何在压痕条款和文本节点的内容处理空白的XDocument。它仍然是标准化的属性,等等。

I believe that LoadOptions.PreserveWhitespace and SaveOptions.DisableFormatting only instruct XDocument on how to handle whitespace in terms of indentation and the content of text nodes. It would still normalize the attributes, etc.

您可能希望在您指定配置为你想要的东西的XmlWriter使用过载,如果你可以'找不到与默认的XmlTextWriter工作的配置,你总是可以创建自己的XmlWriter。

You may wish to use an overload where you specify an XmlWriter that is configured to do what you want, and if you can't find a configuration that works with the default XmlTextWriter, you could always create your own XmlWriter.

这篇关于的XDocument保存保存标签内的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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