Saturday, March 14, 2009

XElement.Value and the new line hell

Ever since I tried XElement, it always made me anxious when I had to workout few lines with the good, old System.Xml.XmlNode. Thanks to LINQ, we got these nice X* family of classes that are so much more easier and helpful.

Recently, we've moved the parts of our cache initialization methods to use XDocument and XElement instead. Some of the cache type classes contain multi-line strings usually displayed in a WinForms TextBox. Accessing the content of an element is simple as using the Value property.

textBoxNotes.Text = MyNode.Element("notes").Value;

In fact, everything works as expected, except the multi-line text. Property Value getter trims off the "\r" character from the string (which is required for the TextBox to actually show the text in the new line) and the lines appeared glued to each other in a single line.

Haven't found anything on this issue yet. It seems annoying to use:

Element("foo").Value.Replace("\n", "\n\r");