Wednesday, 14 December 2011

How to prevent xml encoding of an entity as attribute value

XmlDocument doc = new XmlDocument();
doc.LoadXml("<book><title>War &amp; Peace</title></book>");
//doc.LoadXml("<book Cost=\"456\"><title>War &amp; Peace</title></book>");

XmlElement root = doc.DocumentElement;

XmlAttribute att = doc.CreateAttribute("Cost");
att.InnerXml = "123 &#8364;";

//will properly set the value of Cost attribute to 123
// <book Cost="123 €"><title>War &amp; Peace</title></book>
// and will overwrite already existing attribute, if any

No comments:

Post a Comment