반응형

안녕하세요. 애드소프트(Addsoft)입니다.

 

오늘은 C# asp.net mvc를 이용하여 구글 RSS Feed를 파싱해 볼까합니다.

 

 

일단 프로젝트를 Addsoft로 생성하였습니다.

프로젝트에 Models폴더에 RSSFeed.cs 파일을 생성합니다.

 

내용으로 다음을 코딩하여 줍니다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Addsoft.Models
{
    public class RSSFeed
    {
        public string Title { get; set; }
        public string ApproxTraffic { get; set; }
        public string Link { get; set; }
        public string Description { get; set; }
        public string PubDate { get; set; }
    }
}

 

Controllers 폴더에 RSSFeedController를 생성하고 다음을 코드를 입력합니다.

using Addsoft.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;

namespace Addsoft.Controllers
{
    public class RSSFeedController : Controller
    {
        // GET: RSSFeed
        public ActionResult Index()
        {
            ViewBag.URL = "https://trends.google.com/trends/trendingsearches/daily/rss?geo=KR";
            return View();
        }

        [HttpPost]
        public ActionResult Index(string RSSURL)
        {
            WebClient wclient = new WebClient();
            wclient.Encoding = Encoding.UTF8;
            string RSSData = wclient.DownloadString(RSSURL);

            //소문자를 읽을 경우 오류로 인식된다.
            XDocument xml = XDocument.Parse(RSSData);

            var RSSFeedData = from x in xml.Descendants("item")
                               select new RSSFeed
                               {
                                   Title = ((string)x.Element("title")),
                                   ApproxTraffic = x.DescendantNodes().ElementAt(3).ToString(),
                                   Link = ((string)x.Element("link")),
                                   Description = ((string)x.Element("description")),
                                   PubDate = ((string)x.Element("pubDate"))
                               };

            ViewBag.RSSFeed = RSSFeedData;
            ViewBag.URL = RSSURL;
            return View();
        }
    }
}

 

Views -> RSSFeed 폴더의 Index.cshtml 파일 추가 합니다.

<!-- products -->
<section class="products py-5">
    <div class="container py-lg-5 py-3">
        <h3 class="heading mb-md-5 mb-4"><strong>구글 트렌드</strong> 일별 인기 급상승 검색어</h3>
        <br />
        @using (Html.BeginForm())
        {
            <input type="URL" name="RSSURL" placeholder="Enter RSS Feed URL Here..." class="form-control" value="@ViewBag.URL" style="width:85%; float: left;" />
        <input type="submit" class="btn btn-danger" style="width: 10%; float: right;"/>

            <br />
        }
        <table class="table table-hover">
                <tr>
                    <th>제목</th>
                    <th>조회수</th>
                    <th>링크</th>
                    <th>설명</th>
                    <th>게시일</th>
                </tr>
                @if (ViewBag.RSSFeed != null)
                {
                    foreach (var item in ViewBag.RSSFeed)
                    {
                        <tr>
                            <td>@item.Title</td>
                            <td>@item.ApproxTraffic</td>
                            <td><a href="@item.Link">@System.Web.HttpUtility.UrlDecode(@item.Link)</a></td>
                            <td>@item.Description</td>
                            <td>@item.PubDate</td>
                        </tr>

                    }
                }
        </table>
    </div>
</section>

저는 bootstrap을 이용했습니다.

필요하시면 다음을 이용하시면됩니다.

코딩위치는 Views => Shared => _Layout.cshtml에 추가하시면 됩니다.

    <link href="@Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/bootstrap.min.js")"></script>

 

 

Views => RSSFeed => index.cshtml을 선택하시고 디버깅 해보시면 실행 결과를 볼 수 있습니다.

 

 

예제는 다음 URL에서 확인 해 보실 수 있습니다.

https://addsoft.co.kr/RSSFeed/Index 

 

애드소프트(Addsoft)

Addsoft는 당신의 비즈니스를 위한 아이디어를 구체적으로 실현할 수 있도록 노력을 더합니다. 작은 아이디어로 시작해 광고를 통한 수익을 창출 할 수 있도록 도와 줍니다. Addsoft는 아이디어를

addsoft.co.kr

 

원하는 내용이 없다고 놀라지 마시고 스크롤 좀만 내려주세요.

검색 후에도 처음 내용과 같이 보이니 스크롤 좀만 내려주세요.

홈페이지 만들다가 멈춰 둔 곳에 추가 해뒀습니다 ^^;

공용 레이아웃으로 배너를 넣어놧더니 수정하기 번거롭네요 ㅎㅎ

맨트가 개발중에 정해진게 없어서 생각나는대로 대충 넣어 놨더니 이상하게 보이네요.  _ _)

 

 

참 십쥬~ 

오늘 시간이 늦어 포스팅을 마치 도록 하겠습니다.

 

 

 

 

감사합니다.

반응형

+ Recent posts