0

My application (desktop c# forms) read a XML file to insert data in a database.

I am getting this error (remote server returned error 401 unauthorized), when executing the .Load method of a XmlDocument object.

 var stream = new FileStream(fileName, FileMode.Open);
 var xmlDoc = new XmlDocument();
 xmlDoc.Load(stream);
 ...

Only some machines, in production infrastructure are getting this error. This could be a missing permission?

3
  • Sure sounds like it. Since it's a desktop application, is it running under the user's credentials or a different account? To put it another way, do different users have the problem, or is solely based on certain machines?
    – Tim
    Nov 29, 2013 at 18:56
  • I'm getting this problem with some users and some workstation machines. I just tried in a server machine in my client and works. It is a problem in the users machines. Nov 29, 2013 at 19:05
  • Does the stack trace in the users machines error have any details other than the 401?
    – Shiva
    Nov 29, 2013 at 19:55

1 Answer 1

1

You need to set the .Credentials property of the XmlResolver for the XmlDocument.

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

 var stream = new FileStream(fileName, FileMode.Open);
 var xmlDoc = new XmlDocument();
 xmlDoc.XmlResolver = resolver;  // Set the resolver.
 xmlDoc.Load(stream);

Docs: system.xml.xmldocument.xmlresolver

2
  • Oh. Where are you loading the file from? Local folder or network folder?
    – Shiva
    Nov 29, 2013 at 19:09
  • From local folders. In this case, from D:\somedirectory partition. Nov 29, 2013 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.