使用 Linq to XML 时返回奇怪的字符

发布于 2024-09-17 09:02:24 字数 154 浏览 2 评论 0原文

我正在尝试编写一些 C# 代码来从 XML 文件读取数据。

我想我应该尝试使用 Linq to XML 来做到这一点。

但是,我提取的值被“\n\t\t\t”等字符包围。

谁能解释为什么我会得到这些字符,以及如何删除它们?

谢谢。

I'm trying to write some C# code to read data from an XML file.

I thought I'd try out Linq to XML to do this.

However, the values I'm pulling out are surrounded by characters like "\n\t\t\t".

Can anyone explain why I'm getting these characters, and how I can remove them?

Thanks.

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

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

发布评论

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

评论(1

稚然 2024-09-24 09:02:24

XML 被认为是一种文档标准。然而,在最早的时候,它被选为一种数据表示语言。它在这个角色上很糟糕,但比当时的其他任何东西都好,所以它成为解决各种数据结构在线问题的首选兔子。空白渗入数据的问题是 XML 面向文档的遗留问题的直接结果。

您看到的字符是元素中包含的空白以及您实际感兴趣的内容。最简单的解决方案(如果可以的话)是将它们从源 XML 文件中删除。如果做不到这一点,大多数 XML 处理器都有某种机制来消除周围的空白。不过,我不知道 LINQ-to-XML 对此有何支持。

经过快速谷歌后,似乎有一个选项 加载 XML 进行 LINQ 处理时保留空格。也许它已在 LoadParse 调用中指定。

XML was conceived as a document standard. In the earliest days, however, it was co-opted as a data representation language. It sucked at that role, but was better than anything else that was around at the time, so it became the go-to bunny for all manner of data-structure-over-the-wire problems. Problems with whitespace creeping into your data are a direct result of XML's document-oriented legacy.

The characters you are seeing are the whitespace contained within the element along with the content you are actually interested in. The simplest solution, if it's an option, is to remove them from the source XML file. Failing that, most XML processors have some kind of mechanism to elide surrounding whitespace. I don't know what kind of support LINQ-to-XML has for this, though.

After a quick Google, there appears to be an option to preserve whitespace when loading XML for LINQ processing. Perhaps it has been specified in the Load or Parse call.

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