我是c#的新手,我试图在点击按钮后将OPML文件的内容加载到treeView中。我阅读了几个关于System.Windows.Form.TreeView
和System.Xml
的教程,进行了很多研究,但我仍然无法将opml文件转换为树视图。我有这个代码返回错误,有人可以帮我吗?
private void button2_Click(object sender, EventArgs e)
{
XmlDocument xd = new XmlDocument();
xd.Load("C:\\subscription.opml");
treeView1.Nodes.Add(new TreeNode("Feeds"));
LoadNode(xd.SelectNodes("//outline), treeView1.Nodes[0]);
}
private void LoadNode(XmlNodeList xmlNodes, TreeNode trvParent)
{
foreach (XmlNode xmlNode in xmlNodes)
{
TreeNode trnNewNode = new TreeNode(xmlNode.Attributes["text"].Value); //error: the exeption nullReference is not managed...
if (xmlNode.Attributes["text"] != null)
trnNewNode.Tag = xmlNode.Attributes["text"].Value;
trnNewNode.Expand();
trvParent.Nodes.Add(trnNewNode);
LoadNode(xmlNode.ChildNodes, trnNewNode);
}
}
以下是文件的内容:
<?xml version="1.0" encoding="utf-8"?>
<opml version="1.1">
<head>
<title>My Subscriptions</title>
</head>
<body>
<outline text="Catégorie1" >
<outline text="Sience" >
<outline text="Science Signaling" xmlUrl="http://stke.sciencemag.org/rss/current.xml" htmlUrl="http://stke.sciencemag.org/" description="Science Signaling, the Signal Transduction Knowledge Environment" language="en" type="RSS" />
</outline>
<outline text="Sport" >
<outline text="L'Equipe.fr Actu Golf" xmlUrl="http://www.lequipe.fr/rss/actu_rss_Golf.xml" htmlUrl="http://www.lequipe.fr" description="L'Equipe.fr, Toute l'actualité du golf" type="RSS" />
<outline text="L'Equipe.fr Actu Rallye" xmlUrl="http://www.lequipe.fr/rss/actu_rss_Rallye.xml" htmlUrl="http://www.lequipe.fr" description="L'Equipe.fr, Toute l'actualité des rallyes" type="RSS" />
</outline>
</outline>
<outline text="Catégorie2" >
<outline text="economy" >
<outline text="Business Daily" xmlUrl="http://downloads.bbc.co.uk/podcasts/worldservice/bizdaily/rss.xml" htmlUrl="http://www.bbc.co.uk/programmes/p002vsxs" description="Examining the big issues facing the global economy, Business Daily demystifies the world of money. From giant industries like aviation and automotive to the smallest scale start-up, Business Daily asks the big questions about free trade, technology and investment. There is also analysis of management and marketing trends, and what business jargon really means - together with reports on business news from around the world via the BBC's global network of reporters." type="RSS" />
<outline text="Business & Economy Coverage | PBS NewsHour Podcast | PBS" xmlUrl="http://www.pbs.org/newshour/rss/podcast_business.xml" htmlUrl="http://www.pbs.org/newshour/topic/business/" description="The latest news, analysis and reporting about Business & Economy from the PBS NewsHour and its website, the feed is updated periodically with interviews, background reports and updates to put the news in a larger context." type="RSS" />
</outline>
<outline text="politic" >
<outline text="Power and Politics with Evan Solomon from the
CBC News Network (Highlights)" xmlUrl="http://www.cbc.ca/podcasting/includes/powerandpolitics.xml" htmlUrl="http://www.cbc.ca/podcasting" description="Get daily access to the movers and shakers when Evan interviews the country's most influential figures. From politicians to writers to leaders from all parts of the country." type="RSS" />
<outline text="Politics Weekly" xmlUrl="http://www.guardian.co.uk/politics/series/politicsweekly/podcast.xml" htmlUrl="http://www.guardian.co.uk/audio" description="Political analysis from the Guardian and Observer's top journalists and commentators" type="RSS" />
</outline>
</outline>
</body>
</opml>
这是我想在treeView中显示的内容:
-Categorie1 -sience Sience Signaling -sport L'Equipe.fr Actu Golf L'Equipe.fr Actu Rally -Categorie2 -economy Business Daily Business & Economy Coverage | PBS NewsHour Podcast | PBS -politic Power and Politics with Evan Solomon from the... Politics Weekly
希望有人可以帮忙!感谢。