/// <summary> /// Performs the search, and returns the results in the range from the start /// and to the page size index. /// </summary> /// <param name="start">Index of where in the total result to return results.</param> /// <param name="pageSize">Number of results to return.</param> /// <returns>List of google site search results.</returns> /// <exception cref="System.NullReferenceException"></exception> /// <exception cref="System.NotSupportedException"></exception> /// <exception cref="System.Net.WebException"></exception> /// <exception cref="System.Xml.XmlException"></exception> /// <exception cref="System.Xml.XPath.XPathException"></exception> public virtual List <GoogleSiteSearchResult> Search(string start, string pageSize) { if (this._results.Count > 0) { this._results.Clear(); } // Check whether or not a Google customer ID has been defined if (string.IsNullOrEmpty(this._googleCXNumber)) { throw new NullReferenceException("Google SiteSearch exception: No CX number have been defined. Make sure the key is added to 'Site config/Search settings'."); } string response = this.DoSearch(this.FormatQueryString(this._googleCXNumber, this._q, start, pageSize, this._site)); GoogleSiteSearchParserResult result = this.ParseResult(response); if (result != null) { this.NextResultPageUrl = result.NextUrl; this.PreviousResultPageUrl = result.PreviousUrl; this.NumberOfHits = result.Hits; this._results = result.Result; } return(this._results); }
/// <summary> /// Parses the specified XML node. /// </summary> /// <param name="xmlNode">The XML node.</param> /// <returns></returns> public virtual GoogleSiteSearchParserResult Parse(XmlNode xmlNode) { GoogleSiteSearchParserResult gssp = new GoogleSiteSearchParserResult(); XmlNode searchResult = GetNode(xmlNode, "RES"); if (searchResult == null || searchResult.ChildNodes.Count <= 0) { return gssp; } foreach (XmlNode r in searchResult.ChildNodes) { if (r.Name == "M") { gssp.Hits = r.InnerText; continue; } if (r.Name == "NB") { XmlNode next = GetNode(r, "NU"); XmlNode previous = GetNode(r, "PU"); if (next != null) { gssp.NextUrl = next.InnerText; } if (previous != null) { gssp.PreviousUrl = previous.InnerText; } continue; } if (r.Name == "R") { GoogleSiteSearchResult parsedResult = new GoogleSiteSearchResult() { Url = GetNode(r, "U").InnerText, Headline = GetNode(r, "T").InnerText, Description = GetNode(r, "S").InnerText, Language = GetNode(r, "LANG").InnerText }; gssp.Result.Add(parsedResult); } } return gssp; }
/// <summary> /// Parses the specified XML node. /// </summary> /// <param name="xmlNode">The XML node.</param> /// <returns></returns> public virtual GoogleSiteSearchParserResult Parse(XmlNode xmlNode) { GoogleSiteSearchParserResult gssp = new GoogleSiteSearchParserResult(); XmlNode searchResult = GetNode(xmlNode, "RES"); if (searchResult == null || searchResult.ChildNodes.Count <= 0) { return(gssp); } foreach (XmlNode r in searchResult.ChildNodes) { if (r.Name == "M") { gssp.Hits = r.InnerText; continue; } if (r.Name == "NB") { XmlNode next = GetNode(r, "NU"); XmlNode previous = GetNode(r, "PU"); if (next != null) { gssp.NextUrl = next.InnerText; } if (previous != null) { gssp.PreviousUrl = previous.InnerText; } continue; } if (r.Name == "R") { GoogleSiteSearchResult parsedResult = new GoogleSiteSearchResult() { Url = GetNode(r, "U").InnerText, Headline = GetNode(r, "T").InnerText, Description = GetNode(r, "S").InnerText, Language = GetNode(r, "LANG").InnerText }; gssp.Result.Add(parsedResult); } } return(gssp); }