Load XML from URL giving error (The remote server returned an error: (401) Unauthorized.)

Oct 7 2009 6:27 AM

 
The XML file is in Sharepoint Document Library.
I have tried following codes but it do not work

StringBuilder oBuilder = new StringBuilder();
StringWriter oStringWriter = new StringWriter(oBuilder);
XmlTextReader oXmlReader = new XmlTextReader(sUrl);
XmlTextWriter oXmlWriter = new XmlTextWriter(oStringWriter);
while (oXmlReader.Read())
{
    oXmlWriter.WriteNode(oXmlReader, true);
}
oXmlReader.Close();
oXmlWriter.Close();
 
string HTMLStr;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sUrl);
request.Credentials = CredentialCache.DefaultCredentials;
using (Stream stream = request.GetResponse().GetResponseStream())
{
    using (StreamReader reader = new StreamReader(stream))
    {
        HTMLStr = reader.ReadToEnd();
    }
}
 

XmlUrlResolver resolver = new XmlUrlResolver();
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri("http://......./"), "Digest", new NetworkCredential("......", "........"));
resolver.Credentials = myCache;
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = resolver;
XmlReader reader = XmlReader.Create(sUrl);
 
The Error which I am getting is
[WebException: The remote server returned an error: (401) Unauthorized.]
   System.Net.HttpWebRequest.GetResponse() +5314029
   System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials) +69
   System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials) +3929371
   System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) +54
   System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext) +144
   System.Xml.XmlReader.Create(String inputUri) +8

Answers (1)