Start New Discussion within our Software Development Community

Hi,

I am writing some code to get some data from google suggest api. I am a noob so have run into a problem as my code is retuning blank data.

Here is my code

public XmlDocument GetData(string phrase)
        {
            XmlDocument theXML = new XmlDocument();
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://google.com/complete/search?output=toolbar&q=" + phrase.ToString());
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // Get the stream associated with the response.
                Stream receiveStream = response.GetResponseStream();
                
                theXML.Load(receiveStream);

                response.Close();

            }
            catch (Exception e)
            {
            }
            return theXML;
        }
        

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            XmlDocument theXML = GetData("keyword");
            XmlNode theXmlNode = theXML.DocumentElement;
            XmlNodeList theXmlNodeList = theXmlNode.SelectNodes("/toplevel/CompleteSuggestion");
            for (int x = 0; x < theXmlNodeList.Count; x++)
            {
                MessageBox.Show(theXmlNodeList.Item(x).InnerText);
            }
        }

I just made it so that it would return the data in a message box for testing purposes. All message boxes are blank so I must have made a mistake somewhere.

Can anyone help please?

Thanks

I tried your code and theXML does contain data.
However, all the values are stored as attributes and therefore there is no InnerText.

Try changing InnerText to InnerXML.

To find out how the returned data is formatted, place a breakpoint after the GetData method and inspect theXML.InnerXML using the XMLVisualizer.

This article has been dead for over six months. Start a new discussion instead.