6

What do I need to do in either my C# code or my XML document so that the XDocument parser reads literal whitespace for Values of XElements?


Background

I have an XML document, part of which looks like this:

    <NewLineString>&#10;&#13;</NewLineString>
    <IndentString>    </IndentString>

I'm adding the values of each XELement to a data dictionary using a LINQ query; the .ForEach part looks like this:

    .ForEach(x => SchemaDictionary.Add(
        LogicHelper.GetEnumValue(x.Name.ToString()), x.Value));    

To test to see if the whitespace values were preserved, I'm printing out a line of the character numbers of each value item in the data dictionary. In the following code, x represents a KeyValuePair and the Aggregate is simply making a string of the character integer values:

x.Value.ToCharArray()
    .Aggregate<char,string>("",(word,c) => word + ((int)c).ToString() + " " )
    ));

I expected to see 10 13 for the <NewLineString> value and 32 32 32 32 for the <IndentString> value. However, nothing was printed for each value (note: other escaped values in the XML such as &lt; printed their character numbers correctly).

What do I need to do in either my C# code or my XML document so that my parser adds the complete whitespace string to the Data Dictionary?

1
  • How are you loading the XML document? Commented Feb 5, 2010 at 19:28

3 Answers 3

9

Try loading your XDocument with the LoadOptions.PreserveWhitespace

Sign up to request clarification or add additional context in comments.

Comments

2

Try loading your document this way.

XmlDocument doc = new XmlDocument();
    doc.PreserveWhitespace = true;
    doc.Load("book.xml");

1 Comment

The original question asked about LINQ XDocuments, not System.Xml XmlDocuments. But my web search for an answer about XmlDocuments linked me to this question, so this answer is useful anyway.
-3

or just modify your input xml to:

<NewLineString>&#10;&#13;</NewLineString>
<IndentString xml:space="preserve">    </IndentString>

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.