C# LINQ to XML 缺少空格字符

发布于 2024-08-30 08:55:15 字数 975 浏览 2 评论 0原文

我“手工”编写一个 XML 文件(即不使用 LINQ to XML),其中有时包含一个包含单个空格字符的打开/关闭标记。查看生成的文件后,一切都显示正确,如下所示......

<Item>
  <ItemNumber>3</ItemNumber>
  <English> </English>
  <Translation>Ignore this one. Do not remove.</Translation>
</Item>

这样做的原因多种多样且无关紧要,它已经完成了。

后来,我使用带有 LINQ to XML 的 C# 程序来读回文件并提取记录……

XElement X_EnglishE = null;  // This is CRAZY
foreach (XElement i in Records)
{
    X_EnglishE = i.Element("English");  // There is only one damned record!
}
string X_English = X_EnglishE.ToString();

并进行测试以确保它与数据库记录相比没有变化。在处理字段具有单个空格字符的项目时,我检测到更改......

+E+ Text[3] English source has been altered:
    Was: >>> <<<
    Now: >>><<<

>>>>>和<<<我添加的部分是为了看看发生了什么(很难看到空格字符)。我已经摆弄过这个,但不明白为什么会这样。这并不是绝对关键,因为该字段还没有被使用,但如果我不明白为什么会这样,我就不能相信 C# 或 LINQ 或任何正在执行此操作的东西。那么这是做什么的以及为什么呢?

I write an XML file "by hand", (i.e. not with LINQ to XML), which sometimes includes an open/close tag containing a single space character. Upon viewing the resulting file, all appears correct, example below...

<Item>
  <ItemNumber>3</ItemNumber>
  <English> </English>
  <Translation>Ignore this one. Do not remove.</Translation>
</Item>

... the reasons for doing this are various and irrelevent, it is done.

Later, I use a C# program with LINQ to XML to read the file back and extract the record...

XElement X_EnglishE = null;  // This is CRAZY
foreach (XElement i in Records)
{
    X_EnglishE = i.Element("English");  // There is only one damned record!
}
string X_English = X_EnglishE.ToString();

... and test to make sure it is unchanged from the database record. I detect a change, when processing Items where the field had the single space character...

+E+ Text[3] English source has been altered:
    Was: >>> <<<
    Now: >>><<<

... the >>> and <<< parts I added to see what was happening, (hard to see space characters). I have fiddled around with this but can't see why this is so. It is not absolutely critical, as the field is not used, (yet), but I cannot trust C# or LINQ or whatever is doing this, if I do not understand why it is so. So what is doing that and why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌化 2024-09-06 08:55:16

在我看来,这与 HTML 中的行为相同,其中前导/尾随空格被折叠到自身中,从而导致您得到一个空字符串。我认为如果将单个空格放入 CDATA 块中,可能会解决它。

This looks to me to be the same as the behaviour you get in HTML, where leading/trailing spaces are folded into themselves, thus resulting in you having an empty string. I think that if you put the single space into a CDATA block, that might resolve it.

烟凡古楼 2024-09-06 08:55:16

在 XML 中,标签前后的空白(如空格)会被忽略。解析后的 XML 会忽略这个单个空格,因为它被视为格式化(因为它周围没有文本),因此它不会出现在输出中。

In XML Whitespace (like a space) is ignored after and before tags. The parsed XML ignores this single space because it's seen as formatting (as there is no text surrounding it) and so it doesn't appear in your output.

动次打次papapa 2024-09-06 08:55:15

加载 XML 字符串时需要保留空格:

XDocument doc = XDocument.Parse(@"
<Item>
    <ItemNumber>3</ItemNumber>
    <English> </English>
    <Translation>Ignore this one. Do not remove.</Translation>
</Item>", LoadOptions.PreserveWhitespace);

string X_English = (string)doc.Root.Element("English");

//  X_English == " "

You need to preserve whitespace when loading the XML string:

XDocument doc = XDocument.Parse(@"
<Item>
    <ItemNumber>3</ItemNumber>
    <English> </English>
    <Translation>Ignore this one. Do not remove.</Translation>
</Item>", LoadOptions.PreserveWhitespace);

string X_English = (string)doc.Root.Element("English");

//  X_English == " "
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文