VBForums >
.NET >
C# > [RESOLVED] [2.0] Small problem converting XML linebreaks into textboxes
Click to See Complete Forum and Search --> : [RESOLVED] [2.0] Small problem converting XML linebreaks into textboxes
ThisIsMyUserName
Aug 11th, 2006, 06:53 AM
Problem:
I'm saving the contents of a multiline textbox to an XML file but when I try to read that XML file it's inserting the actual character for a linebreak rather than creating a line break (it shows a square).
If I open the XML file in notepad it's showing the true line break like it should.
How do I fix this so my textboxes also show the true line break?
ComputerJy
Aug 11th, 2006, 09:45 AM
Do you have the "Multilines" property enabled for your text-boxes?
ThisIsMyUserName
Aug 11th, 2006, 02:00 PM
I'm saving the contents of a multiline textbox
Yes. :D
ThisIsMyUserName
Aug 12th, 2006, 05:11 PM
Anyone have a clue why this would happen? I'm using standard serialization on the XML file. I google'd/MSDN'd everything I could think of and no luck.
The contents of the XML tag are being stored into string variables before they are put in the textbox as well.
ComputerJy
Aug 12th, 2006, 05:15 PM
I don't know about a real solution
But you can go for a work-around:
"when you read the string from the XML file, replace all of the unknown char occurrences with line breaks"
ThisIsMyUserName
Aug 12th, 2006, 05:51 PM
Tried that before posting. :(
It's on drugs. The square is actually a carriage return according to ultra edit's "character property" feature (unless ultraedit is converting the unknown char to a valid CR automatically). Character #13.
Negative0
Aug 14th, 2006, 01:08 PM
Are you storing it as CDATA in the XML file?
ThisIsMyUserName
Aug 15th, 2006, 05:27 PM
Nope, here's the code I'm using:
Reading the XML file:
public Settings Load()
{
Settings settings = new Settings();
if (File.Exists(SettingsPath))
{
try
{
using (FileStream fs = new FileStream(SettingsPath, FileMode.Open))
{
XmlSerializer xs = new XmlSerializer(typeof(Settings));
settings = (Settings)xs.Deserialize(fs);
fs.Close();
}
}
catch (Exception e)
{
{
if (MessageBox.Show("Error:\r\n" + e.Message, "Error reading settings file", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
{
if (File.Exists(SettingsPath)) File.Delete(SettingsPath);
}
else
{
Environment.Exit(-1);
}
}
}
}
return settings;
}
Writing the XML file:
public void Save(Settings settings)
{
try
{
using (FileStream fs = new FileStream(SettingsPath, FileMode.Create))
{
XmlSerializer xs = new XmlSerializer(typeof(Settings));
xs.Serialize(fs, settings);
fs.Close();
}
}
catch (Exception e)
{
MessageBox.Show("Error:\r\n" + e.Message, "Error writing settings file", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK);
}
}
"Settings" is the name of my class that has this code. It's consisted of a few static structs which get stored in the XML file.
For displaying the values, I'm just using standard code.
On text change I'm saving the contents of the textbox to a string variable within the struct. Then when the program closes I'm saving all the contents of each struct to the XML file.
ThisIsMyUserName
Aug 22nd, 2006, 12:17 AM
Last bump. I've pretty much given up, and since it's on the 2nd page maybe someone else who had this problem didn't get a chance to see it.
popskie
Aug 24th, 2006, 04:30 AM
saving to xml i did this by changing \n to <br>. Retreive then put to textbox with mutiline replace <br> to \n
Just try
Popskie
popskie
Aug 24th, 2006, 04:35 AM
saving to xml i did this by changing \n to <br>. Retreive then put to textbox with mutiline replace <br> to \n
sorry i thought it is asp.net forums.
ThisIsMyUserName
Aug 24th, 2006, 03:32 PM
Yep, seems the textbox control is a little picky with linebreak syntax.
I had to change \n into \r\n when I read the file. \n by itself didn't work (tried this originally, under the assumption the square was an unknown char).
popskie
Aug 24th, 2006, 09:33 PM
What control you used in displaying that text?
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.