Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I preserve whitespace characters when parsing XML from C# LINQ

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?

like image 218
Ben McCormack Avatar asked Feb 05 '10 19:02

Ben McCormack


People also ask

How do you preserve a space in XML?

This behavior is based on the XSLT specification, which defines that the whitespace will be preserved if: An ancestor element of the text node has an xml:space attribute with a value of preserve , and no closer ancestor element has xml:space with a value of default .

Can XML attribute values have spaces?

Parameter names must be legitimate XML names. A simple attribute value, even that of an xsl:param element's name attribute, may contain whitespace and be perfectly well-formed as XML .

What do you mean by whitespace in XML?

White space is used in XML for readability and has no business meaning. Input XML messages can include line breaks, blanks lines, and spaces between tags (all shown in the following example). If you process XML messages that contain any of these spaces, they are represented as elements in the message tree.


2 Answers

Try loading your XDocument with the LoadOptions.PreserveWhitespace

like image 76
Jeff Cyr Avatar answered Nov 01 '22 15:11

Jeff Cyr


Try loading your document this way.

XmlDocument doc = new XmlDocument();
    doc.PreserveWhitespace = true;
    doc.Load("book.xml");
like image 27
Mitchel Sellers Avatar answered Nov 01 '22 14:11

Mitchel Sellers