A
Andrew
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.ToString());
// 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.ToString());
}
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.ToString());
// 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.ToString());
}