views:

62

answers:

3

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?

+2  A: 

This looks to me to be the same as the behaviour you get in HTML, where leading/trailing spaces are folded into themselves, thus resulting in you having an empty string. I think that if you put the single space into a CDATA block, that might resolve it.

Rob
+2  A: 

In XML Whitespace (like a space) is ignored after and before tags. The parsed XML ignores this single space because it's seen as formatting (as there is no text surrounding it) and so it doesn't appear in your output.

dbemerlin
+2  A: 

You need to preserve whitespace when loading the XML string:

XDocument doc = XDocument.Parse(@"
<Item>
    <ItemNumber>3</ItemNumber>
    <English> </English>
    <Translation>Ignore this one. Do not remove.</Translation>
</Item>", LoadOptions.PreserveWhitespace);

string X_English = (string)doc.Root.Element("English");

//  X_English == " "
dtb
Allowed me to go in the right direction. Issue can be considered to be closed.
Fossaw