Check xml through dtd, another directory for dtd

I am trying to validate an XML file using .dtd. I wrote this validator:

public bool Validation(XmlDocument xmlDoc) { var xml = XmldocToString(xmlDoc); var r = new XmlTextReader(new StringReader(xml)); var settings = new XmlReaderSettings(); var sb = new StringBuilder(); settings.ProhibitDtd = false; settings.ValidationType = ValidationType.DTD; settings.ValidationEventHandler += (a, e) => { sb.AppendLine(e.Message); _isValid = false; }; XmlReader validator = XmlReader.Create(r, settings); while (validator.Read()) { } validator.Close(); return _isValid; } 

The problem is that I have to have the dtd file in the bin directory of the solution. I want to select a different directory to store the .dtd file, and I really cannot find it.

Thank you for your time.

+4
source share
1 answer

Declare a link to the DTD in the Xml file:

Example if dtd is stored on a remote server:

 <!DOCTYPE Catalog PUBLIC "abc/Catalog" "http://xyz.abc.org/dtds/catalog.dtd"> 

Take a look at this wiki page and this site for more options and information on Xml files and the DTD association.

An example if dtd is placed locally (SYSTEM):

Link to DTD from the document :

Assuming the top element of the document is spec , and dtd is located in the mydtd file in the dtds subdirectory of the directory where the document was downloaded from :

 <!DOCTYPE spec SYSTEM "dtds/mydtd"> 

Notes

The system string is actually a URI reference (as defined in RFC 2396), so you can use the full URL string indicating the location of your DTD on the Internet. This is really good if you want others to confirm your document. It is also possible to associate a PUBLIC identifier (magic string) so that DTDs can be viewed in directories on the client side without having to find it on the Internet. A DTD contains a set of declarations of elements and attributes, but they do not determine what the root of the document should be. This explicitly told the parser / validator as the first element of the DOCTYPE declaration.

(Excerpt from here )

+2
source

Source: https://habr.com/ru/post/1445000/


All Articles