windows 从 XML 节点中删除空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1440417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove Whitespace From XML Nodes
提问by THE DOCTOR
How can I remove whitespace on every instance of a particular node which I specify in C#? For example let's say that I have the following XML document:
如何删除我在 C# 中指定的特定节点的每个实例上的空格?例如,假设我有以下 XML 文档:
<XML_Doc>
<Record_1>
<Name>Bob</Name>
<ID_Number>12345</ID_Number>
<Sample>
</Sample>
</Record_1>
<Record_2>
<Name>John</Name>
<ID_Number>54321</ID_Number>
<Sample>
</Sample>
</Record_2>
</XML_Doc>
What I would like is to take every instance of the <Sample>
tag and change the formatting so it looks like this:
我想要的是获取<Sample>
标签的每个实例并更改格式,使其看起来像这样:
<XML_Doc>
<Record_1>
<Name>Bob</Name>
<ID_Number>12345</ID_Number>
<Sample></Sample>
</Record_1>
<Record_2>
<Name>John</Name>
<ID_Number>54321</ID_Number>
<Sample></Sample>
</Record_2>
</XML_Doc>
EDIT:
编辑:
The other application which makes use of the XML file was not written by me and I cannot modify the structure of the XML document itself. I am writing a utility which parses the XML file and replaces each instance of the node I specify. The example where the <Sample></Sample>
tag is on the same line is how the document is originally formatted and how I need it to be for the other application to be able to read it correctly.
另一个使用 XML 文件的应用程序不是我编写的,我无法修改 XML 文档本身的结构。我正在编写一个实用程序来解析 XML 文件并替换我指定的节点的每个实例。<Sample></Sample>
标签位于同一行的示例是文档最初的格式以及我需要它如何让其他应用程序能够正确读取它。
回答by Blue Toque
If you're interested in preserving whitespace (ie: tabs, carriage returns and other formats) you can use the CDATA(unparsed character data).
如果您对保留空格(即:制表符、回车符和其他格式)感兴趣,您可以使用CDATA(未解析的字符数据)。
<![CDATA[
]]>
However, if you just want to have an XML document that is formatted a certain way for aesthetic purposes , I would advise you to leave it alone.
但是,如果您只想拥有一个出于美学目的而以某种方式格式化的 XML 文档,我建议您不要管它。
To write a CDATA section into an XML Document use the following code:
要将 CDATA 部分写入 XML 文档,请使用以下代码:
XmlNode itemDescription = doc.CreateElement("description");
XmlCDataSection cdata = doc.CreateCDataSection("<P>hello world</P>");
itemDescription.AppendChild(cdata);
item.AppendChild(itemDescription);
This produces
这产生
<description><![CDATA[<P>hello world</P>]]></description>
回答by esac
I think you want to set
我想你想设置
xml.Settings.NewLineHandling = NewLineHandling.Entitize;
回答by Al.
I would suggest using an xslt transform. Doing it this way allows you to have control over the stripping or preservation of white space.
我建议使用 xslt 转换。这样做可以让您控制空白的剥离或保留。
MSDN has an article that addresses this issue, see: Controlling White Space with the DOM
MSDN 有一篇文章解决了这个问题,请参阅:Controlling White Space with the DOM
回答by Blue Toque
Based on your comments, I think you maybe able to use the XmlWriterSettings.NewLineHandlingproperty to change how the text node handles the carriage returns. You'll have to experiment. I suspect the receiving application has some line parsing issue and is looking for a \r\n or something.
根据您的评论,我认为您可以使用XmlWriterSettings.NewLineHandling属性来更改文本节点处理回车的方式。你必须试验。我怀疑接收应用程序有一些线路解析问题,正在寻找 \r\n 或其他东西。