gpt4 book ai didi

C# 解析 RSS feed

转载 作者:行者123 更新时间:2023-11-30 17:18:48 24 4
gpt4 key购买 nike

我在从 rss feed 的元素获取字符串时遇到问题。

我在这里遇到错误:

XmlNode sizeNode = xmlnode.SelectSingleNode("torrent:contentLength");

如果我省略该行,代码就会运行。这是其余部分:

 private void button1_Click(object sender, EventArgs e)
{
var xmlDoc = new XmlDocument();
xmlDoc.Load("movies.xml");

var xmlNode = xmlDoc.SelectSingleNode("//rss/channel");
var i = 0;
foreach (XmlNode xmlnode in xmlNode)
{

XmlNode titleNode = xmlnode.SelectSingleNode("title");
XmlNode categoryNode = xmlnode.SelectSingleNode("category");
XmlNode dateNode = xmlnode.SelectSingleNode("pubDate");
XmlNode sizeNode = xmlnode.SelectSingleNode("torrent:contentLength");

if ((titleNode != null) && (dateNode != null) &&
(categoryNode != null) && (sizeNode != null) )
{
DgvMain.Rows.Add();
DgvMain.Rows[i].Cells[1].Value = titleNode.InnerText;
DgvMain.Rows[i].Cells[2].Value = categoryNode.InnerText;
DgvMain.Rows[i].Cells[3].Value = dateNode.InnerText;
DgvMain.Rows[i].Cells[4].Value = sizeNode.InnerText;
i++;
}

}

这是 xml 文件的一部分:

<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:torrent="http://xmlns.ezrss.it/0.1/"> <channel>
<title>movies torrents RSS feed - KickassTorrents</title>
<link>http://kickass.to/</link>
<description>movies torrents RSS feed</description>
<item>
<title>Al Murray My Gaff My Rules 2003 Xvid Mp3.avi</title>

<category>Movies</category>
<author>http://kickass.to/user/Stull/</author> <link>http://kickass.to/al-murray-my-gaff-my-rules-2003-xvid-mp3-avi-t10314315.html</link>
<guid>http://kickass.to/al-murray-my-gaff-my-rules-2003-xvid-mp3-avi-t10314315.html</guid>
<pubDate>Sat, 07 Mar 2015 00:27:56 +0000</pubDate>
<torrent:contentLength>548352170</torrent:contentLength>
<torrent:infoHash>EDBA9CB0942265440B3A0AC6FD70B39CD4384E54</torrent:infoHash>
<torrent:magnetURI><![CDATA[magnet:?xt=urn:btih:EDBA9CB0942265440B3A0AC6FD70B39CD4384E54&dn=al+murray+my+gaff+my+rules+2003+xvid+mp3+avi&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.com%3A1337]]></torrent:magnetURI>
<torrent:seeds>1</torrent:seeds>
<torrent:peers>1</torrent:peers>
<torrent:verified>0</torrent:verified>
<torrent:fileName>al.murray.my.gaff.my.rules.2003.xvid.mp3.avi.torrent</torrent:fileName>
<enclosure url="http://torcache.net/torrent/EDBA9CB0942265440B3A0AC6FD70B39CD4384E54.torrent?title=[kickass.to]al.murray.my.gaff.my.rules.2003.xvid.mp3.avi"

length="548352170" type="application/x-bittorrent" />

最佳答案

由于 contentLength 有前缀“torrent:”,因此在尝试提取它时会出现问题。

几周前我也遇到了同样的问题。
我的解决方案是将 XML 模式转换为一个类,然后将您获取的 XML 数据反序列化为 RSS 对象,这样您就将拥有 XML 模式的所有属性。

第 1 步:将架构转换为类

Visual studio 通过使用以下方式提供此功能:
编辑 > 选择性粘贴 > 将 XML 粘贴为类

另一种转换方法是使用http://xmltocsharp.azurewebsites.net/

第 2 步:使用 System.Xml.Serialization 中的 XmlSerializer 反序列化;

第 3 步:使用 RSS 对象访问所需的属性。

这些类将如下所示:

[XmlRoot(ElementName = "enclosure")]
public class Enclosure
{
[XmlAttribute(AttributeName = "url")]
public string Url { get; set; }
[XmlAttribute(AttributeName = "length")]
public string Length { get; set; }
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
}

[XmlRoot(ElementName = "item")]
public class Item
{
[XmlElement(ElementName = "title")]
public string Title { get; set; }
[XmlElement(ElementName = "category")]
public string Category { get; set; }
[XmlElement(ElementName = "author")]
public string Author { get; set; }
[XmlElement(ElementName = "link")]
public string Link { get; set; }
[XmlElement(ElementName = "guid")]
public string Guid { get; set; }
[XmlElement(ElementName = "pubDate")]
public string PubDate { get; set; }
[XmlElement(ElementName = "contentLength", Namespace = "//kastatic.com/xmlns/0.1/")]
public string ContentLength { get; set; }
[XmlElement(ElementName = "infoHash", Namespace = "//kastatic.com/xmlns/0.1/")]
public string InfoHash { get; set; }
[XmlElement(ElementName = "magnetURI", Namespace = "//kastatic.com/xmlns/0.1/")]
public string MagnetURI { get; set; }
[XmlElement(ElementName = "seeds", Namespace = "//kastatic.com/xmlns/0.1/")]
public string Seeds { get; set; }
[XmlElement(ElementName = "peers", Namespace = "//kastatic.com/xmlns/0.1/")]
public string Peers { get; set; }
[XmlElement(ElementName = "verified", Namespace = "//kastatic.com/xmlns/0.1/")]
public string Verified { get; set; }
[XmlElement(ElementName = "fileName", Namespace = "//kastatic.com/xmlns/0.1/")]
public string FileName { get; set; }
[XmlElement(ElementName = "enclosure")]
public Enclosure Enclosure { get; set; }
}

[XmlRoot(ElementName = "channel")]
public class Channel
{
[XmlElement(ElementName = "title")]
public string Title { get; set; }
[XmlElement(ElementName = "link")]
public string Link { get; set; }
[XmlElement(ElementName = "description")]
public string Description { get; set; }
[XmlElement(ElementName = "item")]
public List<Item> Item { get; set; }
}

[XmlRoot(ElementName = "rss")]
public class KatRss
{
[XmlElement(ElementName = "channel")]
public Channel Channel { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
[XmlAttribute(AttributeName = "torrent", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Torrent { get; set; }
}

省略任何您不需要的属性。

关于C# 解析 RSS feed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29072173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com