Process XML in C# using external entity file -
i processing xml file (which not contain dtd or ent declarations) in c# contains entities such é
, à
. receive following exception when attempting load xml file...
xmldocument xmldoc = new xmldocument(); xmldoc.loadxml(record);
reference undeclared entity 'eacute'.
i able track down proper ent file here. how tell xmldocument
use ent file when loading xml file?
in versions of framework prior .net 4 use prohibitdtd
of 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é</txt></xml>"); xmldocument xd = new xmldocument(); xd.load(xmlreader.create(new memorystream( utf8encoding.utf8.getbytes(xml)), settings));
from .net 4.0 onward use dtdprocessing
property value of dtdprocessing.parse
set on xmltextreader.
xmldocument xd = new xmldocument(); using (var rdr = new xmltextreader(new stringreader(xml))) { rdr.dtdprocessing = dtdprocessing.parse; xd.load(rdr); }
Comments
Post a Comment