Deserializing whitespace into custom C# string class from XML

A

Al

Hi,

Here's a 'simplified' description of the problem.

A.
We created a custom string class in C# to be used for serializing &
deserializing of objects to/from XML.
A skeleton of such a class appears below:
---------------------------
public class CustomString
{
/// <summary>
/// Hidden representation of the actual data.
/// </summary>
protected string strData;

/// <summary>
/// The only public property of this class to be
automatically
/// used by XmlSerializer when de/serializing.
/// </summary>
/// [XmlText] tag below is the only way to ensure that
/// the no <_for_serializer_only_> </_for_serializer_only_>
appear
/// in the output XML.
[XmlText]
public string _for_serializer_only_
{
get { return this.strData; }
set { this.strData = value; }
}

/// <summary>
/// Exposed setter method.
/// </summary>
/// <param name="newValue"></param>
public void SetData( string newValue )
{
this.strData = newValue;
}

/// <summary>
/// Exposed getter method.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.strData;
}
}
---------------------------

B.
The purpose of a custom class is to ensure that developers are not
forgetting to perform various encoding/decoding operations on the data
being used. Therefore all access is via setter/getter methods.

C.
The only exposed public property _for_serializer_only_ above is used
implicitly by XmlSerializer - no developer is allowed to use the
property in their code.

D.
The goal is to have all C# classes that deal with strings utilize
CustomString instead. A sample class appears below:

[XmlRootAttribute( "Person" )]
public class Test
{
protected CustomString m_strName;
protected int m_intAge;

public CustomString Name
{
get { return this.m_strName; }
set { this.m_strName = value; }
}

public int Age
{
get { return this.m_intAge; }
set { this.m_intAge = value; }
}
}

E.
When serializing the class above, the desired output XML should be:
<Person>
<Name>John</Name>
<Age>20</Age>
</Person>

Please note, [XmlText] tag that precedes CustomString's public
_for_serializer_only_ property allows for the suppression of
<_for_serializer_only_> & </_for_serializer_only_> tags around string
'John' above. If one were to use [XmlElement] instead of [XmlText],
the resulting (and not desired) XML would be:
<Person>
<Name>
<_for_serializer_only_>John</_for_serializer_only_>
</Name>
<Age>20</Age>
</Person>

F.
All is working fine except when only a sequence of spaces is present.
At serialization time with ' ' assigned to m_strName member
variable, the resulting and correct XML is:
<Person>
<Name> </Name>
<Age>20</Age>
</Person>

When the XML above is deserialized back, the resulting object's
m_strName is null. It seems that the deserializer trims all of the
spaces and decides not to set the underlying CustomString to spaces.
This is obviously not something that we'd like to have - instead, we'd
prefer to have the _for_serializer_only_ property setter invoked with
spaces, just like is being done for non-whitespace strings.

Below is the code that can be used to demonstrate the issue at hand:

string strFileName = "c:\\test.xml";
CustomString custom = new CustomString();
custom.SetData( " " ); // *** Sequence of spaces.

Test test = new Test();
test.Age = 20;
test.Name = custom;

// Serialize object to file.
//--------------------------
XmlSerializer mySerializer = new
XmlSerializer( typeof( Test ) );
StreamWriter myWriter = new StreamWriter( strFileName );
mySerializer.Serialize( myWriter, test );
myWriter.Close();
//--------------------------

// Deserialize object from file.
//------------------------------
Stream reader = new FileStream( strFileName,
FileMode.Open );
Test test2;
test2 = (Test)mySerializer.Deserialize( reader );
reader.Close();
//------------------------------

Thanks a lot for any ideas or suggestions!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top