views:

1222

answers:

3

I'm triying to validate a XML file. I'm using this code

XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.ValidationType = ValidationType.DTD;

settings.ValidationEventHandler += new ValidationEventHandler(validationError);        

XmlSchemaSet schemas = new XmlSchemaSet();
settings.Schemas = schemas;
XmlReader reader = XmlReader.Create(lblXmlPath.Text, settings);

reader.Settings.Schemas.Add(null, lblDTDPath.Text);
while (reader.Read())
{ 
          // empty by now
}
reader.Close();

But in the line "reader.Settings.Schemas.Add(null, lblDTDPath.Text);" Visual Studio show me that error "For security reasons DTD is prohibited in this XML document. To enable DTD processing set the ProhibitDtd property on XmlReaderSettings to false and pass the settings into XmlReader.Create method"

As you can see in the code, ProhibitDtd is setted to false (I verified during debug too). I also tried to add the Schema before call to XmlReader.Create() with no success.

A: 

From my poking around the only way i could get it to work is not to add the schema to the XmlReader. The DTD specified in the xml document must be a valid url and the XmlReader will download it each time.

If you need the shema to be local, you can change the url of the DTD to point to a local file using a regular expression so it would look somthing like

<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.3//EN" "file:C:\wml.dtd">

Note the file: in the url. Do this in memory before passing it to the XmlReader and that way you do not have to modify the xml file just to varify that it is correct.

Geoff
but I want the user to select from the HD both files, the XML and the dtd, and tell him if the XML is valid or not. I'm pretty sure that it can be done, but I don't know how :(
Jonathan
Updated my aswer with a possible solution to load the DTD from a local file.
Geoff
+1  A: 

Try adding to your DTD schema to schemas collection before the call to XmlReader.Create.

XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.ValidationType = ValidationType.DTD;

settings.ValidationEventHandler += new ValidationEventHandler(validationError);        

XmlSchemaSet schemas = new XmlSchemaSet();

schemas.Add(null, lblDTDPath.Text);
settings.Schemas = schemas;

XmlReader reader = XmlReader.Create(lblXmlPath.Text, settings);

while (reader.Read())
{ 
          // empty by now
}
reader.Close();
Zach Bonham
I also tried ir before (and now again) with no luck. I obtain the same error. It's a little frustrating xD
Jonathan
@Johnathan Maybe, after creating your XmlReader (in your original sample above), check the value of **XmlReader.Settings.ProhibitDtd** and see if it truly is false? From your question, I'm not sure if that was the value you were checking, or the value on the initial settings.ProhibitDtd. Just make sure they are showing the same value. If the reader does't, try setting it to false before loading your schemas? You've probably already tried it but there is nothing else that looks like it might even be an issue?
Zach Bonham
@Jonathan: Did you actually manage to get this to work with a .dtd file? I don't think XmlSchemaSet can be used with a .dtd file... Only .xsd files. I keep getting that error message, no matter what I try.
fretje
@fretje: I think you're right. I'm getting that error too.
Jonathan
@Jonathan: Then why is this the accepted answer?
fretje
I didn't try it again until now. We did a workarround. I tried it yesterday because you left a comment. No answers for months, so I accepted de more detailed question.
Jonathan
A: 

I had a similar problem. The answer, for me, was that the DTD dosn't need to be hooked up via the Schemas as the xml file points to ut,

Adding via schemas caused me the issue.

AJM
I will try it as soon as posible. Thanks.
Jonathan