简体   繁体   中英

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

I have the following code to load an XML doc from a website (hosted by one.com). Issue: i get an error "Unable to connect to the remote server". I checked several posted regarding the same error message, but the suggestions don't work. If I enter the URL in my web browser it views the XML file.

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

First download the XML data and then load them in the XmlDocument object

    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

this is because there must be proxy set in your browser and while accessing the xml file using your code you are not using proxy.

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

Thanks for the answers. I've tried both, but it still didn't work. I had a chat with an operator of one.com and it appears they don't support asp, .NET, C#. So, that's why neither of the above codes are running.

Thanks anyway for the effort.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM