2009-12-11 66 views
1

我已经看过很多用于验证DTD的XML文件的示例,但没有找到允许我使用代理的示例。我有如下,我想验证一个CXML文件(以下简称为显示屏):使用代理验证针对DTD的Xml文件。 C#2.0

<?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> 

我想创建一个简单的C#程序来验证对DTD的XML。我试图代码如下所示,但无法弄清楚如何得到它使用代理:

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); 
} 

我收到的例外是

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

发生就行while (validator.Read()) ;

我知道我可以在本地验证DTD,但我不想更改xml DOCTYPE,因为这是最终形式需要的(此应用仅用于诊断目的)。有关cXML规范的更多信息,您可以访问cxml.org

我很感激任何帮助。

谢谢

回答

1

自从你的问题已经有一段时间了,所以很抱歉,如果有点晚!

这里是似乎是批准办法做到这一点:

1 - 创建你自己的代理组件:

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 - 将所需的密钥到你的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 - 将在defaultProxy节到你的web.config

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

现在,来自应用程序的所有请求都将通过代理。这是所有请求 - 即我不认为你可以选择使用这个编程,资源请求将尝试通过代理!例如:使用dtd文档,webservice调用等验证xml

干杯, 兰斯