Click here to Skip to main content
15,881,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to read xml from web response and get selected nodes from it. This is what I have so far and its throwing me a blank web page.

WRequest method, sends a POST request to url using web request and returns a string xml response such as:
HTML
<status> <code>201</code>
<resources_created>
<link href="####" rel="############" title="####" /> 
</resources_created> 
<warnings> <warning>display_date is read-only</warning> </warnings> 
</status>

C#
public static string readUri2()
        {
            string uri = "";
            string xml = WRequest();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);

            XmlNodeList nodes = xmlDoc.SelectNodes(@"Status");
            foreach (XmlNode node in nodes)
            {
                string tempf = node["resources_created"].InnerText;
                uri = tempf;
            };
            return uri;
        }


C#
protected void Page_Load(object sender, EventArgs e)
        {
            string uri = readUri2();
            Label1.Text = Server.HtmlEncode(uri);

        }

Any help would be very much appreciated. Many thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 23-Sep-14 11:16am    
How about using the debugger to see what's returned from your readUri2 method?
—SA

1 solution

You should use a Stream Reader to obtain the data from the Webrequest. Here is an example you can adapt to your code.

private void Main_Load(object sender, EventArgs e)
{
    WebRequest request = WebRequest.Create("Your XML File Location Here");
    request.Credentials = CredentialCache.DefaultCredentials;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream iDataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(iDataStream);
    string eresponse = reader.ReadToEnd();
    MessageBox.Show(eresponse);
    reader.Close();
    iDataStream.Close();
    response.Close();
}

You can see the original answer to this similar question on this other question which you may find helpful. Source
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900