4

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?

1
  • Love the comments in your code. Commented Apr 29, 2010 at 13:34

3 Answers 3

2

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 == " "
Sign up to request clarification or add additional context in comments.

1 Comment

Allowed me to go in the right direction. Issue can be considered to be closed.
2

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.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.