blue_jack
blue_jack

Reputation: 61

Load external dtd with XmlReader

I created an XML document named test1.xml that links to external dtd mydtd2.dtd which has an entity circ defined. Both files are saved in same folder. But when reading the XML file with XmlReader I get error Reference to undeclared entity circ.

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE test1 SYSTEM "mydtd2.dtd">
 <test1> 
   print this character &circ; 
</test1>

<!ELEMENT test1 >
<!ENTITY circ "&#x0005E;">



 XmlReaderSettings settings = new XmlReaderSettings();
 settings.DtdProcessing = DtdProcessing.Parse;
 settings.CheckCharacters = false;
 XmlDocument doc = new XmlDocument();

 using (XmlReader reader = XmlReader.Create(filename, settings))
 {
    doc.Load(reader);
 }

When I add the entity to the top of the XML file internally it works.

<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE wow [
  <!ENTITY circ     "&#x0005E;" >
]>

<test1> 
     wow can this work ( j &circ;y )
</test1>

Upvotes: 3

Views: 2317

Answers (1)

blue_jack
blue_jack

Reputation: 61

I added this to the settings.

  XmlUrlResolver resolver = new XmlUrlResolver();
    resolver.Credentials = CredentialCache.DefaultCredentials;
    settings.XmlResolver = resolver;

Upvotes: 2

Related Questions