如何在XML C#中删除不必要的硬输入? [英] How do I remove unnecessary hard enters in XML C#?
问题描述
您好!
我的XML包含各种< bold>和< italic>标签。
我用
Xdocument.Load(filename,LoadOptions.PreserveWhiteSpace)
加载文件,但删除标签并将其写入另一个文件后,所有< bold>和< italic>标签被替换为(\ n)。
例如,
< text > < 粗体 > 文字< / bold > < span class =code-keyword>< text >
是:
< text >
文字
< / text >
< text > < 粗体 > < italic > 文本< / bold > < / italic < span class =code-keyword>> < text >
是:
< text >
文字
< / text >
我它是:
< text > 文本< / text >
请帮忙。
问候
Aman
我的尝试: >
Xdocument.Load(filename,LoadOptions.PreserveWhiteSpace)
这就是 LoadOptions.PreserveWhitespace [ ^ ] - 它会保留输入文件中所有无关紧要的空白字符。空白是在开头或结尾,或者连续不止一个;换行''\\ n';和标签'\t'
删除该选项,它可能会消失。如果没有,您需要仔细查看输入文件。
在加载时指定LoadOptions.PreserveWhiteSpace
将不会添加或删除空格,所以我认为这不是问题所在。
我认为你的问题在于保存。
看一下 SaveOptions枚举(System.Xml.Linq)| Microsoft Docs [ ^ ]。
这是XDocument.Save $ c上的可选参数$ c>。
具体来说,你需要DisableFormatting
标志,以确保作者不会插入无关紧要的空格。
但在所有情况下,您都需要学习调试。不要试图只看输入和输出,然后随机调整一些代码。单步执行调试器中的相关代码并观察它。
加载后会出现额外的换行符吗?如果是,你需要研究如何正确加载它。
删除粗体标签后它们是否存在?如果是这样,你需要查看代码进行替换并尝试提出解决方案。
如果你在调用save时元素中没有它们,然后它通过保存添加,你需要查看你传递的标志来保存。
小心Visual Studio中的调试器,它试图通过有时候将新行渲染为空格来帮助你。单击值旁边放大镜显示的小下拉菜单,使用文本可视化工具。
最有可能使用此标记是您的正确方法案件。但是,如果您负责生成XML文件,并且希望最大限度地减少其他工具在处理文件时出现类似错误,那么您应该查看xml:space
属性。它告诉任何符合标准的XML编写器/加载,以便在加载和保存时保留重要的空白,而不需要任何其他参数。如果你是懒惰的话,在根元素处指定它(并且想要让它不太可能在某个地方忘记它),或者如果你想让它保持好,它仍然可以尽可能地格式化。
Quote:是:
< text >
文字
< / text >
我希望它是:
< text > 文字< / text > ;
你的xml结构正是它的结构是,因为......
引用:删除标签并将其写入另一个文件后,所有< ;粗体>和< italic> 标签被(\ n) 取代。
结论:替换带有空字符串的不必要标签。
Hello!
I have an XML that contains various <bold> and <italic> tags.
I am loading the file with
Xdocument.Load(filename, LoadOptions.PreserveWhiteSpace)
, but after removing the tags and writing it to another file, all the <bold> and <italic> tags are getting replaced by ("\n").
For example,
<text><bold>Text</bold><text>
is:
<text>
Text
</text>
<text><bold><italic>Text</bold></italic><text>
is:
<text>
Text
</text>
I want it to be:
<text>Text</text>
Please help.
Regards
Aman
What I have tried:
Xdocument.Load(filename, LoadOptions.PreserveWhiteSpace)
That's what LoadOptions.PreserveWhitespace[^] does - it preserves all the insignificant whitespace characters in the input file. And whitespace is ' ' at the beginning or end, or more than one ' ' in a row; newlines '\n'; and tabs '\t'
Remove the option, and it'll probably disappear. If it doesn't, you need to look very closely at your input file.
When you specifyLoadOptions.PreserveWhiteSpace
while loading there will be no whitespace added or removed, so I do not think this is where the problem is.
I think your problem is on saving.
Take a look at the SaveOptions Enum (System.Xml.Linq) | Microsoft Docs[^].
It is an optional parameter onXDocument.Save
.
Specifically you would need theDisableFormatting
flag to ensure the writer doesn't insert insignificant whitespaces.
But in all cases, you need to learn to debug. Do not try to just look at the input and output and then randomly tweak some code. Single step over the relevant code in the debugger and observe it.
Are the extra newlines present after load? If yes, you need to look into how to load it correctly.
Are they present after removing the bold tags etc? If so, you need to look into the code doing the replacement and try to come up with a solution.
If they are not present in the element when you call save, then it is added by save and you need to look into the flags you pass on to save.
Be careful with the debugger in Visual Studio, it tries to "help" you by rendering new lines as spaces sometimes. Use the "text visualizer" available by clicking the small dropdown menu shown with a magnifying glass next to the value.
Most likely using this flag is the correct approach in your case. But if you are responsible for generating the XML files, and you want to minimize other tools making similar errors processing your files "down the line", you should look into thexml:space
attribute. It tells any standard compliant XML writer/loaded to preserve the significant whitespaces on both load and save without any additional parameters being needed. Specify it at the root element if you are lazy (and want to make it less likely you forget it somewhere), or to individual elements if you want to keep it "nice" where it can still format as much as possible.
Quote:is:
<text> Text </text>
I want it to be:
<text>Text</text>
Your xml structure is exactly what it is, because...
Quote:after removing the tags and writing it to another file, all the <bold> and <italic> tags are getting replaced by ("\n").
Conclusion: replace unnecessary tags with empty string.
这篇关于如何在XML C#中删除不必要的硬输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!