Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process XML in C# using external entity file

I am processing an XML file (which does not contain any dtd or ent declarations) in C# that contains entities such as é and à. I receive the following exception when attempting to load an XML file...

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

Reference to undeclared entity 'eacute'.

I was able to track down the proper ent file here. How do I tell XmlDocument to use this ent file when loading my XML file?

like image 752
Ryan Berger Avatar asked Feb 09 '11 15:02

Ryan Berger


People also ask

What is XML C?

c. Similar to the GLib Markup parser, which also just parses an xml subset, xml. c is a simple, small and self contained xml parser in one file.

What are the functions of XML processor?

An XML processor allows you to work with XML documents and their content. Most XML processors are automated tools, part of larger Enterprise Resource Planning (ERP) systems. They perform actions such as transforming content, updating databases, executing work processes, and delivering content to users.

What file is XML?

An XML file is an extensible markup language file, and it is used to structure data for storage and transport. In an XML file, there are both tags and text. The tags provide the structure to the data. The text in the file that you wish to store is surrounded by these tags, which adhere to specific syntax guidelines.


1 Answers

In versions of the framework prior to .Net 4 you use ProhibitDtd of an XmlReaderSettings instance.

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));

From .Net 4.0 onward use the DtdProcessing property with a value of DtdProcessing.Parse which you set on the XmlTextReader.

XmlDocument xd = new XmlDocument();
using (var rdr = new XmlTextReader(new StringReader(xml)))
{
    rdr.DtdProcessing = DtdProcessing.Parse;
    xd.Load(rdr);
}     
like image 124
rene Avatar answered Oct 13 '22 01:10

rene