tags:

views:

33

answers:

1

Hi,

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.

+1  A: 

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.

Marcelo Cantos
Thanks for your answer. I've tried both LoadOptions.PreserveWhitespace and LoadOptions.None, but neither resolves this problem.Is the answer really to have no indentation in the XML file?
Matt
It might help to see the XML file, or a relevant snippet.
Marcelo Cantos
No problem - here's the content:<?xml version="1.0" encoding="utf-8" standalone="yes"?><customObjects> <customObject> <objectType> TextBox </objectType> <objectName> foreName </objectName> <objectCaption> Forename: </objectCaption> <objectLength> 30 </objectLength> </customObject> <customObject> <objectType> TextBox </objectType> <objectName> surName </objectName> <objectCaption> Surname: </objectCaption> <objectLength> 40 </objectLength> </customObject> </customObjects>
Matt
That doesn't display very well here unfortunately.
Matt
The answer turned out to be very simple - I just had to trim the values.
Matt
Yes, trimming works, but you shouldn't have to. BTW, you should have posted the XML as an amendment to your question, with four-space indentation. Since the comment field collapses all whitespace, it completely defeats the purpose of pasting the XML there.
Marcelo Cantos
Well, Marcel, if I shouldn't have to trim the values, what is going wrong? I will try adding the XML as an amendment to my question as requested.
Matt
I've tried adding the XML to my original question, but even with 4 space indentation it wont post properly.
Matt