开发者

Preserve carriage return when using XDocument.Parse

开发者 https://www.devze.com 2022-12-22 04:51 出处:网络
I have an application that is sensitive to a carriage return being \\r\\n or \\n. I\'m passing around a value in XML and when I parse it using XDocument the carriage retrun value is being converted to

I have an application that is sensitive to a carriage return being \r\n or \n. I'm passing around a value in XML and when I parse it using XDocument the carriage retrun value is being converted to \n and I'm trying to find a way to keep it preserved as \r\n.

string开发者_开发问答 myVal = "1234\r\nabcd";
string xmlText = "<doc>" + myVal + "</doc>";
XDocument xDoc = XDocument.Parse(xmlText);
Console.WriteLine("result=" + (xDoc.Element("doc").Value == myVal));
Console.WriteLine("result=" + (xDoc.Element("doc").Value == myVal.Replace("\r\n", "\n")));

Results:

result=False
result=True


Passing LoadOptions.PreserveWhitespace to XDocument.Parse is supposed to preserve insignificant whitespace from but from the community content at the bottom it appears not to be the case.

Can you load into the XDocument using an XmlReader instead? That may offer much more flexibility.

0

精彩评论

暂无评论...
验证码 换一张
取 消