[英]Validate an Xml file against a DTD with a proxy. C# 2.0
我看過許多針對DTD驗證XML文件的示例,但沒有找到允許我使用代理的示例。 我有一個以下要驗證的cXml文件(為顯示縮寫):
<?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-創建自己的代理程序集:
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文檔,Web服務調用等驗證xml。
干杯,蘭斯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.