Tuesday, October 11, 2005

How to ignore DTD validation

Question:


When I'm trying to load XML into XmlDocument:
xmlDocument.Load(memoryStream);
or deserialize XML:
XmlSerializer reader = new XmlSerializer(responseType);
object response = reader.Deserialize(xmlReader);

I'm getting an error:
Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\MySchema.dtd'

The problem is the "MySchema.dtd" is referred in XML string/stream which I’m trying to load:
-------- XML string ------


------------------------------
I have MySchema.dtd, but I don’t want to deploy it into .NET folder “'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\”

How could I specify folder where Xml parser should look for DTD schemas?

Or at least how to tell XML parsers to ignore DTD validation comletely?

Solution


You may try couple of solutions:
1) Turn off DTD validation completely.
That’s simple: just set XmlResolver property to null:
xmlDocument.XmlResolver = null;
or:
xmlReader.XmlResolver = null;
2) Specify your folder where you store your DTD schemas.
In order to implement it you have to create custom XmlResolver class, that is derive this custom class from XmlResolver, or even better from XmlUrlResolver.
Then override ResolveUri and/or GetEntity methods.
Overridden ResolveUri has to return absolute path to your DTD storage folder.
Last step would be instantiate your custom class and set XmlResolver property:
xmlDocument.XmlResolver = new MyCustomXmlUriResolver();

-------
Example/Examples/Sample for Visual Studio .NET 2005, C# 2.0, ASP.NET 2.0
System.Xml namespace

No comments:

Followers

About Me

My photo
Email me: blog@postjobfree.com