You have to use web request to get the xml data from the url...
Try this below code..
Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://............/../..abc.php?name=xyz&id=123"), HttpWebRequest)
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Dim resStream As New StreamReader(response.GetResponseStream())
Dim doc As New XmlDocument()
Dim xmlResult As String = Nothing
xmlResult = resStream.ReadToEnd()
doc.LoadXml(xmlResult)
prince2485
Member
289 Points
257 Posts
how to read data from xml file from another website,asp.net
May 07, 2012 07:31 AM|LINK
guys,
i m working on a application where i m passing paramter to a http url(http://............/../..abc.php?name=xyz&id=123)
in return ,it display data in a xml format(xml file) , structure are same but data are different,so i have to use loop.
but when i use
var doc = XDocument.Load(http://............/../..abc.php?name=xyz&id=123);
foreach (XElement node in doc.Root.Nodes())
{
Console.WriteLine("name:{0} | value:{1}", node.Name, node.Value);
}
but it returns me a error :
The remote server returned an error: (401) Unauthorized.
but when i execute that http url on browser,it works and returns data in a xml format.
vijay_myl
Contributor
5070 Points
1068 Posts
Re: how to read data from xml file from another website,asp.net
May 07, 2012 07:50 AM|LINK
hi..
You have to use web request to get the xml data from the url...
Try this below code..
Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://............/../..abc.php?name=xyz&id=123"), HttpWebRequest) Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse) Dim resStream As New StreamReader(response.GetResponseStream()) Dim doc As New XmlDocument() Dim xmlResult As String = Nothing xmlResult = resStream.ReadToEnd() doc.LoadXml(xmlResult)My .NET blog
Submit Article
prince2485
Member
289 Points
257 Posts
Re: how to read data from xml file from another website,asp.net
May 07, 2012 08:33 AM|LINK
tx for your code, but can u give a sample code in c#,asp.net
tx in adv
prince2485
Member
289 Points
257 Posts
Re: how to read data from xml file from another website,asp.net
May 07, 2012 09:20 AM|LINK
here is my updated code:
string apiUrl = "http://............/../..abc.php?name=xyz&id=123";
Uri address = new Uri(apiUrl);
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request = (HttpWebRequest)HttpWebRequest.Create(address);
request.Method = "GET";
request.ContentType = "text/xml";
Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader reStream = new StreamReader(request.GetRequestStream(), enc);
string xmlresult = "";
XmlDocument doc = new XmlDocument();
xmlresult = reStream.ReadToEnd();
doc.LoadXml(xmlresult);
but still it gives folowwing error,
"Cannot send a content-body with this verb-type."
vijay_myl
Contributor
5070 Points
1068 Posts
Re: how to read data from xml file from another website,asp.net
May 07, 2012 09:24 AM|LINK
hi..
Try thsi code..
string xmlResult = null; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader resStream = new StreamReader (response.GetResponseStream()); XmlDocument doc = new XmlDocument(); xmlResult = resStream.ReadToEnd(); doc.LoadXml(xmlResult);My .NET blog
Submit Article
prince2485
Member
289 Points
257 Posts
Re: how to read data from xml file from another website,asp.net
May 07, 2012 10:53 AM|LINK
tx for wonderful code,
but i m getting a error in intial stage,
as the site which m exceuting from code behind, required password and username..
i mean,while exceuting that url ma application should pass username and password(set inside code)
and then proceeeds ahead ,this all should happen from code behind.
ramiramilu
All-Star
95403 Points
14096 Posts
Re: how to read data from xml file from another website,asp.net
May 07, 2012 11:26 AM|LINK
USe WebClient and set those credentials and then ping the url to get data - http://msdn.microsoft.com/en-us/library/system.net.webclient.credentials.aspx
Thanks,
JumpStart
Frank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: how to read data from xml file from another website,asp.net
May 09, 2012 05:42 AM|LINK
Try to pass a Client_id in the url:
http://forums.asp.net/t/1754055.aspx/1
Or refer to the sample posts:
http://stackoverflow.com/questions/9587037/best-way-to-retrieve-an-xml-from-an-http-request-in-asp-net
Feedback to us
Develop and promote your apps in Windows Store