C# LINQ to XML missing space character.

Posted by Fossaw on Stack Overflow See other posts from Stack Overflow or by Fossaw
Published on 2010-04-29T13:24:31Z Indexed on 2010/04/29 13:37 UTC
Read the original article Hit count: 388

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-xml