avoid .net XmlTextWriter escaping ampersand in front of character reference



Can someone please be explain why System.Xml.XmlTextWriter escapes the ampersand in front of what I thought was a valid unicode character reference:



[TestMethod]
public void TestEncoding()
{
using (StringWriter sw = new StringWriter())
{
XmlWriter writer = new XmlTextWriter(sw);
const string testChar = "–";
writer.WriteString(testChar);
Assert.AreEqual(testChar, sw.ToString());
writer.Close();
}
}


fails with the message



Assert.AreEqual failed. Expected:<&#8211;>. Actual:<&amp;#8211;>.


Is there a way to escape this behavior, while correctly maintaining conversion of isolated or invalid ampersands to &amp; ?


No comments:

Post a Comment