I ran into the same problem and didn't want to change my XML (or DTD), I decided to create my own XmlResolver to add objects on the fly.
My implementation actually reads the entities from the configuration file, but that should be enough to do what you ask for. In this example, I convert the right single curly quote to an apostrophe.
class XmlEntityResolver : XmlResolver { public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn) { if (absoluteUri.toString() == "-//MY PUB ID") { MemoryStream ms = new MemoryStream(); StreamWriter sw = new StreamWriter(ms); sw.Write("<!ENTITY rsquo \"'\">"); sw.Flush(); ms.Position = 0; return ms; } else { return base.GetEntity(absoluteUri, role, ofObjectToReturn); } } }
Then, when you declare your XmlDocument, just install the converter before loading.
XmlDocument doc = new XmlDocument(); doc.XmlResolver = new XmlEntityResolver(); doc.Load(XML_FILE);
Keith berard
source share