From within my .aspx page I am trying to make a request to an xml page that is on the web and return the value of one of the nodes. The page in question will be a service that accepts a query string and outputs the results to my own aspx page.
For learning purposes though I am just trying to make a simple example. I have found this page: http://www.w3schools.com/xml/note.xml What I would like to do is have a button that when clicked will display to a textbox the value of the < body>< /body> node?
I have been trying to do it with the WebClient Class but I'm not positive if this is the correct way to go about it. I have been following this example http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=33798 but I am encountering exception (407) Proxy Authentication Required.
You could use LINQ to XML like so to load the XML and retrieve the elements you wish:
XDocument document = XDocument.Load("http://www.w3schools.com/xml/note.xml");
string xml = document.Root.ToString();
Using your example (http://www.w3schools.com/xml/note.xml), the above would output the following:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Hope this helps.
Edit (Based on comment)
If you are sitting behind a proxy server and have default credentials setup you can try the following (untested as not behind proxy):
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.w3schools.com/xml/note.xml");
webRequest.Proxy = WebRequest.DefaultWebProxy;
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()))
{
XDocument document = XDocument.Load(new StringReader(streamReader.ReadToEnd()));
string xml = document.Root.ToString();
MessageBox.Show(xml);
}
}
Note
(From MSDN WebRequest.DefaultWebProxy Property )
The DefaultWebProxy property reads proxy settings from the app.config file. If there is no config file, the current user's Internet Explorer (IE) proxy settings are used.
I think you can load the XML into an XmlDataDocument like this:
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.Load("http://mydomain.com/exportsearch?param=SearchText");
Once you have the XML in the document, it should be fairly easy to query it.
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.