简体   繁体   中英

How to use XmlNode.SelectSingleNode in different RSS format?

I am getting data from news website RSS. I got it from about 20 news websites. But, 1 website RSS is diffrent. 20 RSS use same format but this website RSS is different. how can I search "entry" nodes in this diffrent RSS. I search this situation on the internet but I can not find the result I want. Can you help me ?

This is normal format of RSS

在此处输入图片说明

This is my code for normal format RSS

XmlDocument xdoc = new XmlDocument();

        xdoc.Load("http://www.milliyet.com.tr/rss/rssNew/gundemRss.xml");

        XmlElement el = (XmlElement)xdoc.SelectSingleNode("/rss");

        if (el != null)
        {
            el.ParentNode.RemoveChild(el);
        }
        XmlNode Haberler = el.SelectSingleNode("channel");

        List<Milliyet> newMilliyet = new List<Milliyet>();


        foreach (XmlNode haber in Haberler.SelectNodes("item"))
        {
            var link = haber.SelectSingleNode("link").InnerText;

            if (MilliyetHaberList.ContainsKey(link))
                continue;

This is diffrent format RSS

在此处输入图片说明

Your error is wrong file format,this way not working because all RSS link not xml file,sometimes rss link return application/rss+xml content type.

for example:NTV

NTV rss link : http://www.ntv.com.tr/gundem.rss

if you use postman and get this link you will see content type:application/rss+xml

you should use HttpRequest like this

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.ntv.com.tr/gundem.rss");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        StreamReader stream = new StreamReader(response.GetResponseStream());

        string responseString = stream.ReadToEnd();

        string xmlString = responseString.Replace("xmlns=\"http://www.w3.org/2005/Atom\"", "");

        XmlDocument xdoc = new XmlDocument();

        xdoc.LoadXml(xmlString);

        var feedNode = xdoc.LastChild;

        XmlNodeList entries = feedNode.SelectNodes("entry");
        List<NTV> NTVNewsList = new List<NTV>();


        foreach (XmlNode entry in entries)
        {
            NTV NTVInstance = new NTV();
            foreach (XmlNode child in entry.ChildNodes)
            {
                string childName = child.Name;
                switch (childName)
                {
                    case "title":
                        NTVInstance.Title = child.InnerText;
                        break;
                    case "summary":
                        NTVInstance.Description = child.InnerText;
                        break;
                    case "published":
                        string dateStr = child.InnerText;
                        NTVInstance.PubDate = Convert.ToDateTime(dateStr);
                        break;
                    case "link":
                        NTVInstance.Link = child.Attributes["href"].Value;
                        NTVInstance.Tags = GetTags(NTVInstance.Link);

                        break;
                    default:
                        break;
                }

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM