Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent DTD download when parsing XML

Tags:

c#

.net

xml

When using XmlDocument.Load , I am finding that if the document refers to a DTD, a connection is made to the provided URI. Is there any way to prevent this from happening?

like image 773
spender Avatar asked Oct 19 '08 01:10

spender


People also ask

Is DTD mandatory for XML?

XML does not require a DTD. When you are experimenting with XML, or when you are working with small XML files, creating DTDs may be a waste of time.

Why is DTD prohibited in this XML document?

Cause: The most common reason for this error is because the user's ISP is trying to be helpful and is sending the user to a friendly DNS failure page instead of letting it just fail when the DNS entry is not valid.

Is DTD necessary?

A DTD defines the structure and the legal elements of an XML document. Because HTML 4.01 was based on the Standard Generalised Markup Language (SGML), referring to a DTD in the DOCTYPE declaration was necessary.

What is DtdProcessing?

It defines the document structure with a list of legal elements. A DTD can be declared inline in your XML document, or as an external reference. By default, NBi doesn't execute DTD commands in the XML file describing the test-suite. The main reason is security.


1 Answers

After some more digging, maybe you should set the XmlResolver property of the XmlReaderSettings object to null.

'The XmlResolver is used to locate and open an XML instance document, or to locate and open any external resources referenced by the XML instance document. This can include entities, DTD, or schemas.'

So the code would look like this:

        XmlReaderSettings settings = new XmlReaderSettings();         settings.XmlResolver = null;         settings.DtdProcessing = DtdProcessing.Parse;         XmlDocument doc = new XmlDocument();         using (StringReader sr = new StringReader(xml))             using (XmlReader reader = XmlReader.Create(sr, settings))             {                 doc.Load(reader);             } 
like image 92
Richard Nienaber Avatar answered Sep 19 '22 22:09

Richard Nienaber