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".
C开发者_如何学编程an anyone explain why I'm getting these characters, and how I can remove them?
Thanks.
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.
精彩评论