Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML DocumentType method CreateDocumentType crashes if DTD is absent .NET C#

I have a problem with generating an XML that needs to be generated automatically and then manually uploaded on a client site for validation.

The specification of the client requires that we include the DTD declaration at the top. That is

<!DOCTYPE TheFile System "TheDTD.dtd" ""> 

So programmatically when I try to generate the XML the line

    XmlDocumentType doctype;
    doctype = doc.CreateDocumentType("TheFile", null, "TheDTD.dtd", null);
    doc.AppendChild(doctype);

Fails and it asks me the "TheDTD.dtd" should exist in the windows directory: C:\Windows\System32\inetsrv

Later if I want to read the XmlDoc the line

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlLocation);

Fails and it asks me the "TheDTD.dtd" should exist in the Project directory where the whole project is stored

I don't want to actually validate the DTD, I just want to reference because the validation happens on the client side. Is there any way to add the line " " in the XML without having to copy the actual .dtd in 2 different locations?

If we make the declaration public and change this to

 <!DOCTYPE TheFile PUBLIC "TheDTD.dtd" ""> 

then everything works fine but the client requires that it should be SYSTEM and not PUBLIC. I wish the CreateDocumentType and the Load method wouldn't crash if the actual .dtd is missing because at this point I don't even have this file and it's not needed for my purposes.

Thank you

like image 751
Nick Avatar asked Jan 16 '23 08:01

Nick


1 Answers

You can set the XmlDocument.XmlResolver property to null. This disable the loading of external XML resources (dtd, schemas).

doc.XmlResolver= null;
like image 84
zegzav Avatar answered Feb 11 '23 19:02

zegzav