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!
C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.
The bitwise OR assignment operator ( |= ) uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable.
In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.
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.
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: ! !
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With