tags:

views:

33

answers:

0

I am processing an XML document who's DTD is on a server that doesn't respond and the request times out.

 XmlReaderSettings settings = new XmlReaderSettings();
 settings.ProhibitDtd = false;
 settings.XmlResolver = new cXMLResolver();
 settings.ValidationEventHandler += ValidationEventHandler;
 settings.ValidationType = ValidationType.DTD;
 using (XmlReader reader = XmlReader.Create(stream, settings))
 {
  doc = new XmlDocument();
  doc.Load(reader);
  ... code ...
 }

So I have a written a replacement resolver which tells it to use a local copy of the DTD if it attempts to hit this daft server.

public class cXMLResolver : XmlUrlResolver
{
 public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
 {
  // If it is a web location - which we can't access
  if (absoluteUri.AbsoluteUri.ToLower().Contains("cxml.org"))
  {
   String dtd = "cxml.dtd";

   //Returns local root path of the site e.g. c:\Web Sites\cXML\Latest\ for DEV machine
   String rootPath = HttpContext.Current.Server.MapPath(null);
   String actualPath = rootPath + "\\" + dtd;

   if (File.Exists(actualPath))
   {
    // Return a file stream to the DTD on the local machine
    return File.Open(actualPath, FileMode.Open);
   }
   else
   {
    throw new cXMLException("DTD not found on local system at "+actualPath, ResponseStatus.InternalServerError);
   }
  }
  return base.GetEntity(absoluteUri, role, ofObjectToReturn);
 }
}

However it still takes a long time, is it still attempting to contact the server? Or could there be another reason why it takes so long?