基本信息
源码名称:C#读取Rss 并解析Xml实例下载
源码大小:1.54M
文件格式:.zip
开发语言:C#
更新时间:2013-01-04
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

asp.net中 读取远程 rss文件并以xml形式展示出来


html代码:

 

<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        <form action="@Url.Action("getrss")" method="post">
            输入Rss地址:<input type="text" name="rssurl" style="width:400px;" value="http://www.xiaohuayoumo.com/plus/rss/1.xml" />&nbsp;<input type="submit" value="读取" />
            <input type="hidden" name="proxyip" />
        </form>
        
    </div>
</body>
</html>

cs代码:

 

    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult GetRss(string rssurl, string proxyip)
        {
            string strXML = "";
            bool fail = true;
            string strencoding = "utf-8";
            try
            {
                if (!string.IsNullOrEmpty(rssurl))
                {
                    XmlDocument xd = new XmlDocument();
                    #region method2
                    System.Net.HttpWebRequest myhttpRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(rssurl);
                    myhttpRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    myhttpRequest.UserAgent = ".NET Framework Test Client";
                    myhttpRequest.Proxy = null; 
                    if (proxyip.Length > 10)
                    {
                        WebProxy myproxy = new WebProxy(proxyip);
                        myhttpRequest.Proxy = myproxy;
                    }

                    System.Net.HttpWebResponse myhttpResponse = (System.Net.HttpWebResponse)myhttpRequest.GetResponse();
                    System.IO.Stream myrssStream = myhttpResponse.GetResponseStream();
                    xd.Load(myrssStream);

                    #endregion

                    if (xd.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
                    {
                        XmlDeclaration decl = (XmlDeclaration)xd.FirstChild;
                        if (decl.Encoding != "")
                        {
                            strencoding = decl.Encoding;
                        }
                    }

                    if (xd.FirstChild.NextSibling.NodeType == XmlNodeType.ProcessingInstruction)
                    {
                        xd.RemoveChild(xd.FirstChild.NextSibling);
                    }

                    strXML = xd.InnerXml;
                }
                else { fail = false; };
            }
            catch (Exception ex)
            {
                fail = false;
            }
            if (fail == false)
            { 
                Response.Write("false");
            }
            return Content(strXML, "text/xml", System.Text.Encoding.GetEncoding(strencoding));
        }

    }