Validate an Xml file against a DTD with a proxy. C# 2.0

Posted by Chris Dunaway on Stack Overflow See other posts from Stack Overflow or by Chris Dunaway
Published on 2009-12-11T15:39:45Z Indexed on 2010/05/04 14:38 UTC
Read the original article Hit count: 296

Filed under:
|
|
|
|

I have looked at many examples for validating an XML file against a DTD, but have not found one that allows me to use a proxy. I have a cXml file as follows (abbreviated for display) which I wish to validate:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.018/InvoiceDetail.dtd">
<cXML payloadID="123456" timestamp="2009-12-10T10:05:30-06:00">
    <!-- content snipped -->
</cXML>

I am trying to create a simple C# program to validate the xml against the DTD. I have tried code such as the following but cannot figure out how to get it to use a proxy:

private static bool isValid = false;

static void Main(string[] args)
{
   try
   {
     XmlTextReader r = new XmlTextReader(args[0]);
     XmlReaderSettings settings = new XmlReaderSettings();

     XmlDocument doc = new XmlDocument();

     settings.ProhibitDtd = false;
     settings.ValidationType = ValidationType.DTD;
     settings.ValidationEventHandler +=  new ValidationEventHandler(v_ValidationEventHandler);

     XmlReader validator = XmlReader.Create(r, settings);

     while (validator.Read()) ;
     validator.Close();

     // Check whether the document is valid or invalid.
     if (isValid)
         Console.WriteLine("Document is valid");
     else
         Console.WriteLine("Document is invalid");
   }
   catch (Exception ex)
   {
     Console.WriteLine(ex.ToString());
   }
}

static void v_ValidationEventHandler(object sender, ValidationEventArgs e)
{
    isValid = false;
    Console.WriteLine("Validation event\n" + e.Message);
}

The exception I receive is

System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.

which occurs on the line while (validator.Read()) ;

I know I can validate against a DTD locally, but I don't want to change the xml DOCTYPE since that is what the final form needs to be (this app is solely for diagnostic purposes). For more information about the cXML spec, you can go to cxml.org.

I appreciate any assistance.

Thanks

© Stack Overflow or respective owner

Related posts about Xml

Related posts about validation