"stax" <ag*********@fdghsgsgs.de> wrote in message news:d6**********@online.de...
One of the both windows linefeed chars get
dumped somewhere down the road.
Look at the XML at each point down the road to
figure out where the problem lies.
can somebody tell me how to serialize/deserialize
a object containing a multi line string using the
XmlSerializer class.
Here's an example,
- - - MultiLineStringSerialization.cs
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class MyObject
{
private string x;
public MyObject( ) { x = ""; }
public string MyProperty {
get { return x; }
set { x = value; }
}
}
public class App
{
public static void Main( )
{
// Here is an object about to be serialized with lots of whitespace and
// blank lines.
//
MyObject goingOut = new MyObject( );
goingOut.MyProperty = "Hello\r\nWorld\r\n\r\nHow are you?";
XmlSerializer xs = new XmlSerializer( typeof( MyObject));
XmlTextWriter xtw = new XmlTextWriter(
new StreamWriter( "serializeMultiLineStrings.xml"));
xs.Serialize( xtw, goingOut);
xtw.Flush( );
xtw.Close( );
//
// Breakpoint here and examine the file produced by Serialize( )
//
XmlTextReader xtr = new XmlTextReader(
new StreamReader( "serializeMultiLineStrings.xml"));
MyObject comingIn = xs.Deserialize( xtr) as MyObject;
// This is what the multi-line string property looks like when done
// deserializing -- see if the blank lines and extra whitespace are
// still there.
//
Console.WriteLine( comingIn.MyProperty);
xtr.Close( );
}
}
- - -
In some situations what you describe sounds like normal Whitespace
normalization. You shouldn't encounter that in simple element content,
because your string's whitespace is significant. If you ever did encounter
this, your remedy is to turn it off by serializing an xml:space='preserve'
attribute; and handling ignoring no whitespace on deserialization.
However, XmlSerializer doesn't require this (wouldn't be very useful if
it couldn't deserialize a string it had serialized into an exact copy, now
would it?)
If you're serializing as an XmlAttribute, then its possible some
consumers may subject you to whitespace normalization if they
read the attribute value as being NMTOKENS (a space-separated
list of different values, in which case you only need one whitespace
to delimit values). XmlSerializer doesn't fall into this camp though,
it escapes newlines (
, 
, etc.). You can apply
[XmlAttribute( )] to MyProperty above and see this for
yourself in the example.
Look to see if you're going through any other XML processing
intermediaries that are doing this whitespace normalization (and
tell them to preserve whitespace, or treat attribute values as
xsd:string).
Derek Harmon