473,395 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

XmlSerializer replacing cr+nl with nl when loading

I am using an XmlSerializer to save some settings and I have discovered that
when a string is saved containing a cr+nl it is replaced with just a newline
when loading back in. I am no expert with XML and I wonder if there is a
option I have to change to fix this. I have written some simple test code
to show the problem.

Can anyone please tell me why the cr+nl is being replaced by just a newline
and how to stop this happening?

Thanks
public class SettingsClass
{
private string _TestString;
public string TestString
{
get
{
return _TestString;
}
set
{
_TestString = value;
}
}
}

private void butTest_Click(object sender, EventArgs e)
{
SettingsClass Settings = new SettingsClass();

Settings.TestString = "abc" + Environment.NewLine + "xyz";

// Save the settings
using (StreamWriter Writer = new
StreamWriter(@"c:\work\test.xml"))
{
XmlSerializer Serializer = new
XmlSerializer(typeof(SettingsClass));

Serializer.Serialize(Writer, Settings);
}

// This shows the string has 8 chars
MessageBox.Show(Settings.TestString.Length.ToStrin g());

// Look at the c:\work\test.xml file at this point and we see it
does
// contain a cr+newline between the abc and xyz

// Load the settings
using (FileStream Reader = new FileStream(
@"c:\work\test.xml", FileMode.Open, FileAccess.Read,
FileShare.Read))
{
XmlSerializer Serializer = new
XmlSerializer(typeof(SettingsClass));

Settings = (SettingsClass)Serializer.Deserialize(Reader);
}

// This shows the string is now only 7 chars in length.
// Only the newline remains, the cr has been stripped out!
MessageBox.Show(Settings.TestString.Length.ToStrin g());
}

--
Andrew Cutforth - AJC Software - www.ajcsoft.com
The best folder synchronize and directory compare tool available.
AJC Active Backup instantly archives every file you edit giving you
unlimited undo and automatic revision control. Never lose your data again.
Sep 2 '08 #1
6 3680
"Andrew" <so*****@nospam.comwrote in message
news:ea**************@TK2MSFTNGP04.phx.gbl...
>I am using an XmlSerializer to save some settings and I have discovered
that when a string is saved containing a cr+nl it is replaced with just a
newline when loading back in. I am no expert with XML and I wonder if
there is a option I have to change to fix this. I have written some simple
test code to show the problem.

Can anyone please tell me why the cr+nl is being replaced by just a
newline and how to stop this happening?

Thanks
public class SettingsClass
{
private string _TestString;
public string TestString
{
get
{
return _TestString;
}
set
{
_TestString = value;
}
}
}

private void butTest_Click(object sender, EventArgs e)
{
SettingsClass Settings = new SettingsClass();

Settings.TestString = "abc" + Environment.NewLine + "xyz";

// Save the settings
using (StreamWriter Writer = new
StreamWriter(@"c:\work\test.xml"))
{
XmlSerializer Serializer = new
XmlSerializer(typeof(SettingsClass));

Serializer.Serialize(Writer, Settings);
}

// This shows the string has 8 chars
MessageBox.Show(Settings.TestString.Length.ToStrin g());

// Look at the c:\work\test.xml file at this point and we see
it does
// contain a cr+newline between the abc and xyz

// Load the settings
using (FileStream Reader = new FileStream(
@"c:\work\test.xml", FileMode.Open, FileAccess.Read,
FileShare.Read))
{
XmlSerializer Serializer = new
XmlSerializer(typeof(SettingsClass));

Settings = (SettingsClass)Serializer.Deserialize(Reader);
}

// This shows the string is now only 7 chars in length.
// Only the newline remains, the cr has been stripped out!
MessageBox.Show(Settings.TestString.Length.ToStrin g());
}
Can't really explain this behavior - it sounds a bit like XML whitespace
normalization and end-of-line handling, but whitespace in serialized strings
should still be handled correctly.

However, here's something from a Google search... try creating an
XmlSerializer on top of XmlWriter rather than directly on top of
StreamWriter. If you don't do that, XmlSerializer creates an XmlWriter
itself, and, apparently, it uses the old obsolete XmlXxxWriter classes
rather than XmlWriter.Create(), which results in different implementation
used.
Sep 2 '08 #2
Andrew wrote:
// Load the settings
using (FileStream Reader = new FileStream(
@"c:\work\test.xml", FileMode.Open, FileAccess.Read,
FileShare.Read))
{
XmlSerializer Serializer = new
XmlSerializer(typeof(SettingsClass));

Settings = (SettingsClass)Serializer.Deserialize(Reader);
}

// This shows the string is now only 7 chars in length.
// Only the newline remains, the cr has been stripped out!
MessageBox.Show(Settings.TestString.Length.ToStrin g());
Try whether using an XmlTextReader with Normalization set to false helps:
XmlTextReader reader = new XmlTextReader(@"c:\work\test.xml");
reader.Normalization = false;
Settings = (SettingsClass)Serializer.Deserialize(reader);

An XML parser is supposed to normalize crlf to lf, see
http://www.w3.org/TR/xml/#sec-line-ends, so you have to explicitly turn
that off if you don't want it.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 2 '08 #3

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Andrew wrote:
> // Load the settings
using (FileStream Reader = new FileStream(
@"c:\work\test.xml", FileMode.Open, FileAccess.Read,
FileShare.Read))
{
XmlSerializer Serializer = new
XmlSerializer(typeof(SettingsClass));

Settings = (SettingsClass)Serializer.Deserialize(Reader);
}

// This shows the string is now only 7 chars in length.
// Only the newline remains, the cr has been stripped out!
MessageBox.Show(Settings.TestString.Length.ToStrin g());

Try whether using an XmlTextReader with Normalization set to false helps:
XmlTextReader reader = new XmlTextReader(@"c:\work\test.xml");
reader.Normalization = false;
Settings = (SettingsClass)Serializer.Deserialize(reader);

An XML parser is supposed to normalize crlf to lf, see
http://www.w3.org/TR/xml/#sec-line-ends, so you have to explicitly turn
that off if you don't want it.
An XML parser is supposed to normalize CR, true, but not if it's entered as
an entity reference - i.e., - and I would fully expect
XmlWriter.WriteString to escape CR using

Of course, I might well be wrong.
Sep 2 '08 #4
Thanks. The following code fixes the problem. I didn't have to set the
Normalization option though. Just using the XmlTextReader seems to be
enough.

// Load the settings
using (FileStream Reader = new FileStream(
@"c:\work\test.xml", FileMode.Open, FileAccess.Read,
FileShare.Read))
{
XmlTextReader XmlReader = new XmlTextReader(Reader);

XmlSerializer Serializer = new
XmlSerializer(typeof(SettingsClass));

Settings = (SettingsClass)Serializer.Deserialize(XmlReader);
}

--
Andrew Cutforth - AJC Software - www.ajcsoft.com
The best folder synchronize and directory compare tool available.
AJC Active Backup instantly archives every file you edit giving you
unlimited undo and automatic revision control. Never lose your data again.

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Andrew wrote:
> // Load the settings
using (FileStream Reader = new FileStream(
@"c:\work\test.xml", FileMode.Open, FileAccess.Read,
FileShare.Read))
{
XmlSerializer Serializer = new
XmlSerializer(typeof(SettingsClass));

Settings = (SettingsClass)Serializer.Deserialize(Reader);
}

// This shows the string is now only 7 chars in length.
// Only the newline remains, the cr has been stripped out!
MessageBox.Show(Settings.TestString.Length.ToStrin g());

Try whether using an XmlTextReader with Normalization set to false helps:
XmlTextReader reader = new XmlTextReader(@"c:\work\test.xml");
reader.Normalization = false;
Settings = (SettingsClass)Serializer.Deserialize(reader);

An XML parser is supposed to normalize crlf to lf, see
http://www.w3.org/TR/xml/#sec-line-ends, so you have to explicitly turn
that off if you don't want it.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Sep 2 '08 #5
Thanks Pavel.

I tried this but the xml file created still has a real cr+lf in the text so
it still loads the same. I was expecting these chars to be represented by
an escape sequence maybe.

Martin's solution to change the reader side did work though.

The question still remains whether the proper way to do it is to have real
cr+lf in the file or an escape sequence?

--
Andrew Cutforth - AJC Software - www.ajcsoft.com
The best folder synchronize and directory compare tool available.
AJC Active Backup instantly archives every file you edit giving you
unlimited undo and automatic revision control. Never lose your data again.

"Pavel Minaev" <in****@gmail.comwrote in message
news:uV**************@TK2MSFTNGP02.phx.gbl...
"Andrew" <so*****@nospam.comwrote in message
news:ea**************@TK2MSFTNGP04.phx.gbl...
>>I am using an XmlSerializer to save some settings and I have discovered
that when a string is saved containing a cr+nl it is replaced with just a
newline when loading back in. I am no expert with XML and I wonder if
there is a option I have to change to fix this. I have written some
simple test code to show the problem.

Can anyone please tell me why the cr+nl is being replaced by just a
newline and how to stop this happening?

Thanks
public class SettingsClass
{
private string _TestString;
public string TestString
{
get
{
return _TestString;
}
set
{
_TestString = value;
}
}
}

private void butTest_Click(object sender, EventArgs e)
{
SettingsClass Settings = new SettingsClass();

Settings.TestString = "abc" + Environment.NewLine + "xyz";

// Save the settings
using (StreamWriter Writer = new
StreamWriter(@"c:\work\test.xml"))
{
XmlSerializer Serializer = new
XmlSerializer(typeof(SettingsClass));

Serializer.Serialize(Writer, Settings);
}

// This shows the string has 8 chars
MessageBox.Show(Settings.TestString.Length.ToStrin g());

// Look at the c:\work\test.xml file at this point and we see
it does
// contain a cr+newline between the abc and xyz

// Load the settings
using (FileStream Reader = new FileStream(
@"c:\work\test.xml", FileMode.Open, FileAccess.Read,
FileShare.Read))
{
XmlSerializer Serializer = new
XmlSerializer(typeof(SettingsClass));

Settings = (SettingsClass)Serializer.Deserialize(Reader);
}

// This shows the string is now only 7 chars in length.
// Only the newline remains, the cr has been stripped out!
MessageBox.Show(Settings.TestString.Length.ToStrin g());
}

Can't really explain this behavior - it sounds a bit like XML whitespace
normalization and end-of-line handling, but whitespace in serialized
strings should still be handled correctly.

However, here's something from a Google search... try creating an
XmlSerializer on top of XmlWriter rather than directly on top of
StreamWriter. If you don't do that, XmlSerializer creates an XmlWriter
itself, and, apparently, it uses the old obsolete XmlXxxWriter classes
rather than XmlWriter.Create(), which results in different implementation
used.


Sep 2 '08 #6
Andrew wrote:
Thanks. The following code fixes the problem. I didn't have to set the
Normalization option though. Just using the XmlTextReader seems to be
enough.
That Normalization property is false by default on XmlTextReader so that
explains why you did not have to set it explicitly to false.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 2 '08 #7

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

Similar topics

3
by: middletree | last post by:
I put in some code to keep apostrophes from being a problem. However, on the display page, they have double quotes. What am I doing wrong? Here is the code on the page which captures the data...
2
by: Chris Aitchison | last post by:
Hello, I am attempting to have a class that I have written serialize so that it can be both passed as a parameter or return value for a webservice, and also be serialized to disk using the...
11
by: Nick Flandry | last post by:
Hi all, When I run this code: XmlSerializer theXS; try { theXS = new XmlSerializer(typeof(TableInfo)); } catch( System.Exception eAll )
1
by: Jamus Sprinson | last post by:
Before I continue, I'm going to begin by saying I'm not by any means an expert- I've been using .NET with C# for about 4 months now, and basically just learning by example and docs. A game...
18
by: Martin Johansen | last post by:
Hello When opening a CR-NL file, ftell returns the length of the file with the CR-NL as two bytes, is it supposed to do so? I am comparing two file-sizes, one CR-NL and one NL using ftell to...
0
by: jon | last post by:
Hi, I have a problem using the XmlSerializer when I switch between two databases (SQL Server and Oracle). Very puzzling! If my app is set for SQL Server it works fine. Basically the app has...
0
by: Raghu | last post by:
SGen can be used manually or through visual studio project build properties (select On for Generate Serialization Assembly). I used SGen manually to generate serializer classes for a type. I...
0
by: Rod | last post by:
I've upgraded a Visual Studio .NET 2003 app (C#) to Visual Studio .NET 2005. It is a WinForms application, and it uses Crystal Reports 11 Release 2. After going through the upgrade process with the...
0
by: theonlydavewilliams | last post by:
Hi there I'm hoping there's an easy answer to a (hopefully) not too long-winded issue... I'm building a C# web client using a proxy wsdl.exe'd from a wsdl file and six schemas, each in a different...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
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,...
0
tracyyun
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...

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.