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

I have looked through many examples for validating an XML file with a DTD, but did not find one that allows me to use a proxy. I have a cXml file as follows (shorthand for display) that I want to test:

<?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 xml against 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 am getting is

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

      

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

I know that I can check the DTD locally, but I don't want to change the xml DOCTYPE as this is what the final form should be (this app is purely for diagnostics). For more information on the cXML specification, you can go to cxml.org .

I appreciate any help.

thanks

+1


a source to share


1 answer


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

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

1 - Create your 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 required keys in your web.config:



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

      

3 - Place the defaultProxy section in 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 - meaning I don't think you can choose to use this programmatically, every resource request will try to go through the proxy! eg: xml validation using dtd docs, webservice calls, etc.

Cheers, Spear

+1


a source







All Articles