472,119 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

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 10096
"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 leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.