Wednesday, January 19, 2011

Read XML from network/ static ip with credentials

When you wants to read/get xml from network or from static ip on web then you would required to validate your credentials for that. below is example from which you can learn that how to access xml from another system with validating credentials.

public XmlDocument getXML()
{
string fileName = "123.22.23.2//folderName//xmlName.xml";
        Uri myUrl = new Uri("file://" + fileName);

        FileWebRequest request = (FileWebRequest)WebRequest.Create(myUrl);
        byte[] authBytes = Encoding.UTF8.GetBytes(("myuserName" + ":" + "MyPassword").ToCharArray());
        request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);
        WebResponse response = request.GetResponse();
        Stream strm = response.GetResponseStream();
        StreamReader sr = new StreamReader(strm, System.Text.Encoding.Default);
        XmlDocument xdoc = new XmlDocument();
        xdoc.LoadXml(sr.ReadToEnd());
        return xdoc;
}

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates