使用 Linq to XML 时返回奇怪的字符
我正在尝试编写一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XML 被认为是一种文档标准。然而,在最早的时候,它被选为一种数据表示语言。它在这个角色上很糟糕,但比当时的其他任何东西都好,所以它成为解决各种数据结构在线问题的首选兔子。空白渗入数据的问题是 XML 面向文档的遗留问题的直接结果。
您看到的字符是元素中包含的空白以及您实际感兴趣的内容。最简单的解决方案(如果可以的话)是将它们从源 XML 文件中删除。如果做不到这一点,大多数 XML 处理器都有某种机制来消除周围的空白。不过,我不知道 LINQ-to-XML 对此有何支持。
经过快速谷歌后,似乎有一个选项 加载 XML 进行 LINQ 处理时保留空格。也许它已在
Load
或Parse
调用中指定。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
orParse
call.