8

I have a XML node with a value which is a white space. Example:

<sampleNode> </sampleNode>

I am using a Serializer to get the data from XML document to store it in an object. Now, the problem I am facing is: If the XML node value contains nothing but a white space, as does the sample node above, the serializer interpretates it as a string.Empty.

How can I overcome this? I need get the actual white space, i.e. " ". Thanks a bunch!

Boris
  • 9,986
  • 34
  • 110
  • 147
  • Thank you Oded, Phil and Laurent for your quick responses and correct answers. All your replies are true - however, my problem is still unresolved. Here's what I am using to convert the XML file to an object: StreamReader SR = new StreamReader(File.Open(Path, FileMode.Open)); XmlSerializer Serializer = new XmlSerializer(typeof(MyType)); MyType obj = (MyType)Serializer.Deserialize(SR); return obj; Any ideas still? – Boris Dec 18 '09 at 09:11

3 Answers3

12

Assuming you are using XmlDocument, you should set the PreserveWhiteSpace property to True.

If using and XmlReader set the WhitespaceHandling property WhitespaceHandling.All.

See this MSDN article about Preserving White Space While Serializing.

The different serializers handle this different ways, try using the XmlTextReader for this, as per this forum post.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Unfortunately, even though your solution is completely correct, I cannot apply it in my code, as I am not using neither XmlDocument nor XmlReader. I have added my code that converts XML file to an object in the post comment. – Boris Dec 18 '09 at 09:13
  • Well, what do you know: I started using XmlReader :D Thanks so much for your answer! – Boris Dec 18 '09 at 09:26
4

Sample class:

using System;

namespace GeneralTesting
{
    [Serializable]
    public class SampleNode
    {
        public string sampleNode = " ";
    }
}

And sample program:

    XmlSerializer xmls = new XmlSerializer(typeof(SampleNode));
    SampleNode sn = new SampleNode();
    using (FileStream fs = File.Open(@"C:\test.xml", FileMode.Create))
    {
        xmls.Serialize(fs, sn);
    }
    using (FileStream fs = File.OpenRead(@"C:\test.xml"))
    {
        XmlReaderSettings xmlrs = new XmlReaderSettings();
        xmlrs.IgnoreWhitespace = false;
        using (XmlReader xmlr = XmlReader.Create(fs, xmlrs))
        {
            Console.WriteLine("!{0}!", ((SampleNode) xmls.Deserialize(xmlr)).sampleNode); //output: ! !
        }
    }
VMAtm
  • 27,943
  • 17
  • 79
  • 125
1

You can use a CDATA placeholder in order to avoid the removal of the space:

<sampleNode><![CDATA[ ]]></sampleNode>
Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
  • Even thought your solution can solve my problem, I will be giving more time to the people to try to overcome the XmlSerializer issue I am facing with a more elegant solution. If however, no one comes up with the better idea, I will mark your answer as correct. – Boris Dec 18 '09 at 09:18
  • Have you tried to use the " " entity ? I just though about it. – Laurent Etiemble Dec 18 '09 at 09:52