473,785 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10413
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
91673
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
2
2199
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
1
3281
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 PC, if a user's writing a line and presses the <Enter> key, the program interprets that as the user...
3
8592
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
23
5336
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 know. The solutions I have seen all assume that the lambda must be completely inlined within the...
11
6253
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 +
6
2948
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) )
0
4805
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 VB.NET you put an " & _ at the end of every line (you have to start and stop the string on each line...
1
3820
by: Jiri Barton | last post by:
Hello, I have a problem with international characters in multi-line strings. Works: '''á''' Works: ''' a'''
0
9647
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4061
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
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.