开发者

How do I request a xml page and parse through it without actually loading it

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 wh开发者_如何学Pythonen 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜