简体   繁体   中英

XmlDocument Load Error 401 Unauthorized

string configFilePath = System.Configuration.ConfigurationManager.AppSettings["PATH"].ToString();
XmlDocument doc = new XmlDocument();
XmlTextReader reader = null;
reader = new XmlTextReader(configFilePath);
doc.Load(reader);
reader.Close();

doc.Load(reader) gives the following error:

The remote server returned an error: (401) Unauthorized.

Edit: The path is http://localhost/something/mydoc.xml and in the filesystem the IIS user has access, also I tried with an "everyone full control" but nothing changed. I tried to put the path into the url box of IExplorer and it shows the xml correctly.

I ran into the same issue, and after some research, came up with a solution (see here , and here ).

In this case, setting the XMLURLResolver solved my issue

XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;                    

XmlReader reader = XmlReader.Create(url);
reader.XmlResolver = resolver;

If the value of "PATH" is trying to connect with any type of remote machine it sounds like it doesn't have access. So, if your "PATH" value is something like "\\ComputerName\\SharedDirectory\\file.xml" be sure the user has permission to the "ComputerName" machine.

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