On Oct 27, 2:26*pm, Martin Honnen <mahotr...@yahoo.dewrote:
No, the XPath data model does not know any DTDs so it does certainly not
help.
If you want the XmlReader (or XmlDocument) to ignore the referenced DTD
then you can try to set the XmlResolver property (of the
XmlReaderSettings you create your XmlReader withhttp://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings....)
to null. That way the reader will not fetch any resources. That will
only work however if the XML document does not references any entities
defined in the DTD.
A bit more work but a more complete solution is to set the XmlResolver
to your own implementation of UrlResolver, for instance by subclassing
XmlUrlResolver, that then uses a locally cached copy of the DTDs.
Thanks for your answer, it took so long before my post came up that I
actually thought it had gone missing at first, only noticed it now!
This is the code I'm using at the moment:
XmlDocument xdoc = new XmlDocument();
bool docIsValid = false;
try
{
xdoc.XmlResolver = null;
xdoc.Load(scorePath);
docIsValid = true;
}
catch (System.Exception ex)
{
errorList.Add(ex.Message);
}
if (docIsValid == true)
{
foreach (XmlNode node in xdoc.ChildNodes)
{
if (node.GetType().ToString().Contains("DocumentType" ))
{
// Delete it
xdoc.RemoveChild(node);
}
}
MemoryStream ms = new MemoryStream();
xdoc.Save(ms);
ms.Position = 0;
XmlReader xmlDoc = XmlReader.Create(ms);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.XmlResolver = new LocalXmlResolver();
settings.ValidationEventHandler += new
System.Xml.Schema.ValidationEventHandler(settings_ ValidationEventHandler);
XmlSchema x =
XmlSchema.Read(Utilities.getSchemaFromResources(pv gschema),
settings_ValidationEventHandler);
settings.Schemas.Add(x);
settings.ValidationType = ValidationType.Schema;
XmlReader reader = XmlReader.Create(xmlDoc, settings);
while (reader.Read())
{
}
}
Basically I want to validate against a locally saved schema (which is
set to an embedded resource), and never validate against the DTD. The
code above is not ideal as I'm validating the xml file twice, once to
remove the DTD reference then once against the schema, however it does
avoid me having to go get all the documents referenced in the DTD
(which could take up to a minute).
Also, I've saved all the schemas referenced to in 'pvgschema' locally
and added them as embedded resources, but it doesn't seem like the
XmlResolver works as I thought as it still does an HTTP get for those
schemas on the line settings.Schemas.Add(x);.
Is there a simpler way of doing this?
Many thanks,
AK