0

这个网站www.samachar.com如何使用 google news rss?

我正在寻找用 asp.net 编码的这个功能。我有一个代码,但它不适用于谷歌新闻 rss。

代码:

public void CreateRSS(String Path, int maxitem1, string opennewwindow,String  lengthoof_decs)

{
    XmlDocument doc = null;
    XmlTextReader rssReader = null;
    Label doclbl = null;
    Label snolbl = null;
    try
    {
        try
        {
            rssReader = new XmlTextReader(Path);
        }
        catch (Exception)
        {
            return;
        }

        if (rssReader == null)
        {
            return;
        }

        doc = new XmlDocument();
        try
        {
            doc.Load(rssReader);
        }
        catch (Exception)
        {
            return;
        }

        if (doc == null)
        {
            return;
        }

        ReadDoc(doc, maxitem1, opennewwindow, lengthoof_decs);
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        doc = null;
        rssReader = null;
        doclbl = null;
        snolbl = null;
    }


}
public void ReadDoc(XmlDocument rssDoc, int maxitem2, string opennewwindow, string  lengthoof_decs)
{
    DataTable dt = new DataTable();

    int maxitems = 0; ;

    dt.Columns.Add("title");
    dt.Columns.Add("desc");
    dt.Columns.Add("url");
    XmlNode nodeRss = null;
    XmlNode nodeChannel = null;
    XmlNode nodeItem = null;
    try
    {
        if (rssDoc == null)
        {
            return;
        }

        // Loop for the <rss> tag
        for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
        {
            // If it is the rss tag
            if (rssDoc.ChildNodes[i].Name == "rss")
            {
                nodeRss = rssDoc.ChildNodes[i];
                break;
            }
        }

        if (nodeRss == null)
        {
            return;
        }
        for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
        {
            if (nodeRss.ChildNodes[i].Name == "channel")
            {
                nodeChannel = nodeRss.ChildNodes[i];
                break;
            }
        }

        if (nodeChannel == null)
        {
            return;
        }

        // Loop for the <title>, <link>, <description> and all the other tags
        for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
        {
            string title = "";
            string desc = "";
            string url="";
            if (nodeChannel.ChildNodes[i].Name == "item")
            {
                nodeItem = nodeChannel.ChildNodes[i];
                if (nodeItem["title"] != null)
                {
                    title = nodeItem.InnerXml.ToString(); ;
                    int Pos1 = title.IndexOf("<title>") + "<title>".Length;
                    int Pos2 = title.IndexOf("</title>");
                    title = title.Substring(Pos1, Pos2 - Pos1);
                    //<![CDATA[
                   title= title.Replace("CDATA", "").Replace ("%","").Replace("<", "").Replace("!", "").Replace("[", "").Replace("<![CDATA[", "").Replace("&nbsp", "").Replace("]]>", "").Replace("]", "").Replace(">", "");
                }
                if (nodeItem["description"] != null) {
                    desc  = nodeItem.InnerXml.ToString(); ;
                    int Pos1 = desc.IndexOf("<description>") + "<description>".Length;
                    int Pos2 = desc.IndexOf("</description>");
                    desc = desc.Substring(Pos1, Pos2 - Pos1);
                    ////////////////////////////////////////////
                    try
                    {
                        int g1 = desc.IndexOf("&lt;img");
                        int g2 = desc.IndexOf("&gt;") + "&gt;".Length;
                        if (g1 > -1 && g2 > -1)
                        {
                            string desc1 = desc;
                            desc1 = desc1.Substring(g1, g2 - g1);
                            desc = desc.Replace(desc1, "");
                        }
                    }
                    catch (Exception ehge)
                    {
                    }

                    try
                    {
                        int g1 = desc.IndexOf("<img");
                        int g2 = desc.IndexOf(">") + ">".Length;
                        if (g1 > -1 && g2 > -1)
                        {
                            string desc1 = desc;
                            desc1 = desc1.Substring(g1, g2 - g1);
                            desc = desc.Replace(desc1, "");
                        }
                    }
                    catch (Exception ehge)
                    {
                    }


                    try
                    {
                        int g1 = desc.IndexOf("&lt;img");
                        int g2 = desc.LastIndexOf("&gt;")+"&gt;".Length;
                        if (g1 > -1 && g2 > -1)
                        {
                            string desc1 = desc;
                            desc1 = desc1.Substring(g1, g2 - g1);
                            desc = desc.Replace(desc1, "");
                        }
                    }
                    catch (Exception ehge)
                    {

                    }
                    for (int d = 0; d <= 5; d++)
                    {
                        try
                        {

                            int g1 = desc.IndexOf("&lt");
                            int g2 = desc.IndexOf("&gt;") + "&gt;".Length;
                            if (g1 > -1 && g2 > -1)
                            {
                                string desc1 = desc;
                                desc1 = desc1.Substring(g1, g2 - g1);
                                desc = desc.Replace(desc1, "");
                            }


                        }
                        catch (Exception ehge)
                        {

                        }
                    }
                    for (int d = 0; d <= 5; d++)
                    {
                        try
                        {

                            int g1 = desc.IndexOf("<");
                            int g2 = desc.IndexOf(">") + ">".Length;
                            if (g1 > -1 && g2 > -1)
                            {
                                string desc1 = desc;
                                desc1 = desc1.Substring(g1, g2 - g1);
                                desc = desc.Replace(desc1, "");
                            }


                        }
                        catch (Exception ehge)
                        {

                        }
                    }

                    ////////////////////////////////////////////


                    desc = desc.Replace("CDATA", "").Replace("<", "").Replace ("%","").Replace("!", "").Replace("[", "").Replace("<![CDATA[", "").Replace("&nbsp", "").Replace("]]>", "").Replace("]", "").Replace(">", ""); ;




                  if (lengthoof_decs.Trim() != "ALL")
                  {
                      try
                      {
                          string tempdesc = desc;
                          tempdesc = tempdesc.Substring(0, Convert.ToInt16(lengthoof_decs));
                          tempdesc = tempdesc + " ..".Replace("CDATA", "").Replace("<", "").Replace("!", "").Replace("[", "").Replace("<![CDATA[", "").Replace("&nbsp", "").Replace("]]>", "").Replace("]", "").Replace(">", "");

                          desc = tempdesc;
                      }
                      catch (Exception ee)
                      {
                         // desc = desc.Substring(Pos1, Pos2 - Pos1);
                      }
                  }


                    url  = nodeItem.InnerXml.ToString(); ;
                    Pos1 = url.IndexOf("<link>") + "<link>".Length;
                    Pos2 = url.IndexOf("</link>");
                    url = url.Substring(Pos1, Pos2 - Pos1);

                    url = url.Replace("amp;", "").Replace("CDATA", "").Replace("<", "").Replace("!", "").Replace("[", "").Replace("<![CDATA[", "").Replace("&nbsp", "").Replace("]]>", "").Replace("]", "").Replace(">", "");



                }
                if (nodeItem["pubDate"] != null) {

                }

                DataRow dr = dt.NewRow();
                dr["title"] = title;
                dr["desc"] = desc;
                dr["url"] = "http://www.desikhabar.in/show/?move="+url; 
                dt.Rows.Add(dr);

                maxitems = maxitems + 1;



                if (maxitems == maxitem2 )
                { break; }
            }


        }
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        this.abc(dt, maxitem2, opennewwindow, lengthoof_decs);


    }
}
public void abc(DataTable dt, int maxiem, string opennewwindow, string   lengthoof_decs)
   {
       try
       {
           if (opennewwindow == "1")
           {
               for (int i = 0; i < maxiem; i++)
               {

                   opennewwindow = "_blank";
                   Response.Write("  <li> <a target='" + opennewwindow + "' class='h-linkn' href='" + dt.Rows[i]["url"].ToString() + "' title='" + dt.Rows[i]["title"].ToString().Replace("'", " ") + "'>" + dt.Rows[i]["title"].ToString() + "</a> </li>");
                   if (lengthoof_decs.Trim () !="0")
                   {
                        Response.Write(" <p>"+dt.Rows[i]["desc"].ToString ()+"</p> ");
                   }

               }
           }
           else
           {
               for (int i = 0; i < maxiem; i++)
               {
                   Response.Write("  <li> <a  class='h-linkn' href='" + dt.Rows[i]["url"].ToString() + "' title='" + dt.Rows[i]["title"].ToString().Replace("'", " ") + "'>" + dt.Rows[i]["title"].ToString() + "</a> </li>");

                   if (lengthoof_decs.Trim() != "0")
                   {
                       Response.Write(" <p>" + dt.Rows[i]["desc"].ToString() + "</p> ");
                   }
               }

           }



       }
       catch (Exception ea)
       {
       }
}
4

1 回答 1

0

如果您要阅读一些标准 Rss,请查看System.ServiceModel.Syndication命名空间中的SyndicationFeed

于 2013-07-22T06:21:25.350 回答