1: static StringBuilder sb = new StringBuilder();
2: static StringWriter sw = new StringWriter(sb);
3: static XmlWriter writer = new XmlTextWriter(sw);
4:
5: protected void Page_Load(object sender, EventArgs e)
6: {
7:
8: String appId = "Tu codigo";
9:
10: String query = "geeks.ms";
11:
12: try
13: {
14: MSNSearchService s = new MSNSearchService();
15: SearchRequest searchRequest = new SearchRequest();
16:
17: SourceRequest[] sr = new SourceRequest[1];
18: sr[0] = new SourceRequest();
19: sr[0].Source = SourceType.Web;
20: sr[0].ResultFields = ResultFieldMask.All;
21: sr[0].Count = 50; // Resultados maximos
22:
23: searchRequest.Query = query;
24: searchRequest.Requests = sr;
25: searchRequest.AppID = appId;
26: searchRequest.CultureInfo = "es-ES";
27: searchRequest.SafeSearch = SafeSearchOptions.Moderate;
28: searchRequest.Flags = SearchFlags.DisableHostCollapsing
29: | SearchFlags.DisableSpellCorrectForSpecialWords | SearchFlags.MarkQueryWords;
30:
31: SearchResponse searchResponse = s.Search(searchRequest);
32:
33: // Obtener el numero total de resultados de la busqueda
34: int totalResults = searchResponse.Responses[0].Total;
35:
36: // Iterar a traves de los resultados
37: foreach (Result sourceResult in searchResponse.Responses[0].Results)
38: {
39: string Titulo, Descripcion, Url;
40:
41: // Sacar los datos solicitados de la busqueda
42: if (!String.IsNullOrEmpty(sourceResult.Title))
43: {
44:
45: Titulo = sourceResult.Title.Replace("xe000", "<b>").Replace("xe001", "</b>");
46:
47: }
48:
49: if (!String.IsNullOrEmpty(sourceResult.Description))
50: {
51: Descripcion = sourceResult.Description.Replace("xe000", "<b>").Replace("xe001", "</b>");
52: }
53:
54: Url = (!String.IsNullOrEmpty(sourceResult.Url)) ? sourceResult.Url : "";
55:
56: // Imprimir resultados
57: _renderResults(writer, sourceResult);
58: }
59: }
60: catch (Exception exception)
61: {
62: //
63: }
64:
65: // a hack to get the search terms bolded in the pretty output.
66: sb = sb.Replace("xe000", "<b>").Replace("xe001", "</b>");
67: Resultados.InnerHtml = sb.ToString();
68:
69: }
70: /// <summary>
71: /// Escribir los resultados en la pagina web
72: /// </summary>
73: /// <param name="writer"></param>
74: /// <param name="result"></param>
75: protected void _renderResults(XmlWriter writer, Result result)
76: {
77: writer.WriteStartElement("p");
78:
79: // Titulo
80: if (!String.IsNullOrEmpty(result.Title))
81: {
82: writer.WriteString("Titulo: " + result.Title);
83: writer.WriteStartElement("br"); writer.WriteEndElement(/*br*/);
84: }
85:
86: // Descripcion
87: if (!String.IsNullOrEmpty(result.Description))
88: {
89: writer.WriteString("Descripción: " + result.Description);
90: writer.WriteStartElement("br"); writer.WriteEndElement(/*br*/);
91: }
92:
93: // Url
94: if (!String.IsNullOrEmpty(result.Url))
95: {
96: writer.WriteString("Url: " + result.Url);
97: }
98:
99: writer.WriteEndElement(/*p*/);
100: writer.WriteStartElement("hr");
101: writer.WriteEndElement(/*hr*/);
102: }