C# load XML: 无法连接到远程服务器

C# load XML: Unable to connect to the remote server

我有以下代码可以从网站(由 one.com 托管)加载 XML 文档。问题:我收到一个错误 "Unable to connect to the remote server"。 我检查了几个关于相同错误消息的帖子,但这些建议不起作用。 如果我在网络浏览器中输入 URL,它会查看 XML 文件。

public partial class WebForm1 : System.Web.UI.Page
    {
            private XmlDocument dbKAA;
            private XmlElement root;

    public WebForm1()
    {

    }
        protected void Page_Load(object sender, EventArgs e)
        {
            //LOAD XML
            XmlDocument dbKAA = new XmlDocument();
            dbKAA.Load("http://www.something.com/XMLfile.xml");
            root = dbKAA.DocumentElement

首先下载XML数据,然后将它们加载到XmlDocument对象中

    HttpClient client = new HttpClient();
    string url = "http://(urlHere)";
    HttpResponseMessage response = await client.GetAsync(url);
    string xmlData = await response.Content.ReadAsStringAsync();
    XmlDocument dbKAA = new XmlDocument();
    dbKAA.Load(xmlData);
    root = dbKAA.DocumentElement

这是因为您的浏览器中必须设置代理,而在使用您的代码访问 xml 文件时您没有使用代理。

WebProxy webpro = new WebProxy(ProxyAddress);
webpro.Credentials = new NetworkCredential(ProxyUID, ProxyPwd);
WebClient wclient = new WebClient(){ Proxy =webpro};
MemoryStream mstream = new MemoryStream(wc.DownloadData("http://www.something.com/XMLfile.xml"));
XmlTextReader xtr = new XmlTextReader(mstream);
XDoc = XDocument.Load(xtr);

感谢您的回答。我都试过了,但还是不行。我与 one.com 的运营商交谈过,他们似乎不支持 asp、.NET、C#。所以,这就是为什么上面的代码都不是 运行.

谢谢你的努力。