Process XML in C # using an external entity file

I am processing an XML file (which does not contain dtd or ent declarations) in C # that contains objects like é and à . When I try to load an XML file, I get the following exception:

 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(record); 

Reference to an undeclared 'Eacute' object.

I managed to find the correct file here . How to tell XmlDocument use this ent file when loading my XML file?

+6
c # xml special-characters
source share
5 answers

In structure versions prior to .Net 4, you use the ProhibitDtd instance of XmlReaderSettings.

 var settings = new XmlReaderSettings(); settings.ProhibitDtd = false; string DTD = @"<!DOCTYPE doc [ <!ENTITY % iso-lat1 PUBLIC ""ISO 8879:1986//ENTITIES Added Latin 1//EN//XML"" ""http://www.oasis-open.org/docbook/xmlcharent/0.3/iso-lat1.ent""> %iso-lat1; ]> "; string xml = string.Concat(DTD,"<xml><txt>ren&eacute;</txt></xml>"); XmlDocument xd = new XmlDocument(); xd.Load(XmlReader.Create(new MemoryStream( UTF8Encoding.UTF8.GetBytes(xml)), settings)); 

Starting with .Net 4.0, use the DtdProcessing property with the DtdProcessing.Parse value that you set in XmlTextReader.

 XmlDocument xd = new XmlDocument(); using (var rdr = new XmlTextReader(new StringReader(xml))) { rdr.DtdProcessing = DtdProcessing.Parse; xd.Load(rdr); } 
+3
source share

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); 
+3
source share

&eacute; by default is not a valid XML object, while by default it is a valid HTML object.

You will need to define &eacute; as a valid XML object for XML parsing purposes.

EDIT:

To add a link to your external file, you need to do this in the XML file itself. Save the ent file to disk and place it in the same directory as the analyzed document.

 <!ENTITY % stuff SYSTEM "iso-lat1.ent"> %stuff; 

If you'd like to take a different route, check out the ENTITY ad information.

+2
source share

According to this , you must reference them in a file; you cannot tell LoadXml to do this for you.

0
source share

In 2004, your question was answered in an MSDN article ........ You can find it here .......

http://msdn.microsoft.com/en-us/library/aa302289.aspx

0
source share

All Articles