views:

303

answers:

1

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"&gt;
<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

A: 

Hi, It's been a while since your question, so sorry if it's a bit late!

Here's what seems to be the approved way to do it:

1 - Create your very own proxy assembly:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Configuration;

namespace ProxyAssembly
{
    public class MyProxy:IWebProxy
    {


#region IWebProxy Members

    ICredentials  IWebProxy.Credentials
    {
        get 
        { 
            return new NetworkCredential(ConfigurationSettings.AppSettings["googleProxyUser"],ConfigurationSettings.AppSettings["googleProxyPassword"],ConfigurationSettings.AppSettings["googleProxyDomain"]); 
        }
        set { }

    }

    public Uri  GetProxy(Uri destination)
    {
        return new Uri(ConfigurationSettings.AppSettings["googleProxyUrl"]);
    }

    public bool  IsBypassed(Uri host)
    {
        return Convert.ToBoolean(ConfigurationSettings.AppSettings["bypassProxy"]);
    }

#endregion
}
}

2 - Put the needed keys into your web.config:

   <add key="googleProxyUrl"  value="http://proxy.that.com:8080"/&gt;
    <add key="googleProxyUser"  value="service"/>
    <add key="googleProxyPassword"  value="BadDay"/>
    <add key="googleProxyDomain"  value="corporation"/>
    <add key="bypassProxy"  value="false"/>

3 - Put in a defaultProxy section into your web.config

<configuration>        
    <system.net>
        <defaultProxy>
            <module type="ProxyAssembly.MyProxy, ProxyAssembly"/>
        </defaultProxy>
    </system.net>    
</configuration>

Now ALL requests from your application will go through the proxy. That's ALL requests - ie I don't think that you can select to use this programatically, every resource request will try to go through the proxy! eg: validating xml using dtd docs, webservice calls, etc.

Cheers, Lance

Lanceomagnifico