473,561 Members | 3,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlSerializer and multi line strings


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.

Nov 12 '05 #1
3 10391
"stax" <ag*********@fd ghsgsgs.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,

- - - MultiLineString Serialization.c s
using System;
using System.IO;
using System.Xml;
using System.Xml.Seri alization;

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.MyProp erty = "Hello\r\nWorld \r\n\r\nHow are you?";

XmlSerializer xs = new XmlSerializer( typeof( MyObject));
XmlTextWriter xtw = new XmlTextWriter(
new StreamWriter( "serializeMulti LineStrings.xml "));

xs.Serialize( xtw, goingOut);

xtw.Flush( );
xtw.Close( );
// Breakpoint here and examine the file produced by Serialize( )
XmlTextReader xtr = new XmlTextReader(
new StreamReader( "serializeMulti LineStrings.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.WriteLi ne( comingIn.MyProp erty);
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='pres erve'
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
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.

Nov 12 '05 #3

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

I stumbled upon it thru a search...

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

Mar 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Doug Tolton | last post by:
I've been using Python for quite some time now, and I'm a bit stumped. Does it really not have a multi-line comment? I think I must just be missing something simple and stupid. Thanks in advance. Doug Tolton
by: Victor Nazarov | last post by:
Assuming my locale has enough info about codepage and multi-byte charecters, how should I compare (collate) multi-byte strings (strings of multi-byte charecters with zero-byte at the end) in ISO C99? Thanks in advance Vir
by: VMI | last post by:
In my Windows app, if I'm in a multi-line textbox and I'm writing a postal address (ie. write ist line and press <Enter>, write 2nd line and press <Enter>, etc...) how can I make sure that the Enter key will ALWAYS make the cursor jump to the following line in the textbox? For some reason, and this happens everywhere except on my development...
by: JimC | last post by:
I think this question has been asked before, but I haven't found a satisfactory answer. Does anyone know how to make a ListView that displays multiple lines in a row? Jim
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes I applied myself to this problem and come up with this off-the-wall proposal for you people. Perhaps this idea has been proposed before, I don't...
by: rossum | last post by:
I want to declare a const multi-line string inside a method, and I am having some problems using Environment.NewLine. I started out with: class foo { public void PrintStuff() { const string multiline = "The first line." + Environment.NewLine +
by: gburdell1 | last post by:
When constructing a particularly long and complicated command to be sent to the shell, I usually do something like this, to make the command as easy as possible to follow: commands.getoutput( 'mycommand -S %d -T %d ' % (s_switch, t_switch) + '-f1 %s -f2 %s ' % (filename1, filename2) + '%s' % (log_filename) )
by: ilcil | last post by:
I was looking for information on how to have strings cross multiple lines in asp.net a result came up from google from this site, but it was an unanswered question from the newsgroup archives. I finally found a solution, and wanted to post it here so it's easier for everyone to fine. Basically you prepend an @ to the string in C#.NET, or in...
by: Jiri Barton | last post by:
Hello, I have a problem with international characters in multi-line strings. Works: '''á''' Works: ''' a'''
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.