Hi,
I am using Linq to XML related classes for processing of XML that i get from a web server. Using the System.Net.HttpWebRequest and related classes i can successfully get the xml as a string. Now, want to convert the string i get into XElement (or XDocument), for which i am using XElement.Parse(string text) method. Then i display the values of some attributes in textboxes.
I had no problems until i found a subtle bug. The problem is that sometimes the WebServer sends XML which has a newline character in some attributes. To give an example the server sends something like this:
<?xml version="1.0" ?><Items><Item Name=\"Item1\" Description=\"First Item\"/><Item Name=\"Item2\" Description=\"Item with \n newline\"/></Items>
If you look at the description of the second item you will see that there is a newline character \n in it. I found out that the server is sending 0x0D 0x0A (carriage return, line feed) sequence which the text visualiser correctly displays as newline.
Now when i try to parse it into xml using XElement.Parse(string text) then i see that the \n has been converted into a space i.e. 0x20 char. So whenever i try to display the value of the Description in a TextBox using something like
int i = 0;
XElement itemElement = XElement.Parse(serverString);
foreach(XElement itemElement in itemElement.Elements("Item"))
{
string description = (string)itemElement.Attribute("Description");
TextBoxList[i].Text = descriptionText;
i++;
}
I get a space instead of a newline in the TextBox. This is because of the \n to 'space' conversion while Xelement.Parse is used.
I know that the WebSever should have send 
 instead of a \n but is there a way to fix it ? I used String.Replace() to replace all \n with 
 but it also relpaces any \n outside the attribute so my original xml becomes
<?xml version="1.0" ?>
<Items>
<Item Name=\"Item1\" Description=\"First Item\"/>
<Item Name=\"Item2\" Description=\"Item with 
 newline\"/>
</Items>
so it does not parse due to 
 present outside the attributes.
Also, it is surprising that the XAttribute does not seem to have this problem
XAttribute xa = new XAttribute("Description", "Item with \n newline");
string attributeValue = (string)xa.Value;
this code works perfectly and value of attributeValue is
Item with \n newline . Is there any way out of this ?