469,356 Members | 2,013 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

XmlSerializer and multi line strings


Hello,

can somebody tell me how to serialize/deserialize
a object containing a multi line string using the
XmlSerializer class. One of the both windows
linefeed chars get dumped somewhere down the road.
It would be awesome somebody could answer this,
sadly most of my questions don't get answered.

Regards,
Frank
Nov 12 '05 #1
3 9662
"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 (&#xA;, &#xD;, 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
Nov 12 '05 #2
thanks a lot, the example worked perfectly.
I compared it to my code and quickly found
what I was doing wrong. I was using FileStream
like the SDK sample instead of XmlTextReader
and XmlTextWriter.

Regards,
Frank
Nov 12 '05 #3

Thanks! I had the same problem. This fixed my problem too.

I stumbled upon it thru a search...

--
gsrrr
------------------------------------------------------------------------
Posted via http://www.mcse.ms
------------------------------------------------------------------------
View this thread: http://www.mcse.ms/message1611302.html

Mar 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Doug Tolton | last post: by
2 posts views Thread by Victor Nazarov | last post: by
1 post views Thread by VMI | last post: by
3 posts views Thread by JimC | last post: by
23 posts views Thread by Kaz Kylheku | last post: by
6 posts views Thread by gburdell1 | last post: by
1 post views Thread by Jiri Barton | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.