opml文件模拟以下default.opml
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head>
<title>NewsFox OPML Export</title>
<dateModified>Fri Dec 16 2016 16:21:16 GMT+0800</dateModified>
</head>
<body>
<outline text="源" type="NFgroup">
<outline type="rss" text="Engadget 中国版" xmlUrl="http://cn.engadget.com/rss.xml" htmlUrl="http://cn.engadget.com" />
<outline type="rss" text="糗事百科" xmlUrl="http://feed.feedsky.com/qiushi" htmlUrl="http://www.qiushibaike.com/" /></outline>
<outline text="科技" type="NFgroup">
<outline type="rss" text="cnBeta.COM业界资讯" xmlUrl="http://fullrss.net/a/http/cnbeta.feedsportal.com/c/34306/f/624776/index.rss" htmlUrl="http://www.cnbeta.com"/>
<outline type="rss" text="小众软件" xmlUrl="http://fullrss.net/a/http/feed.appinn.com/" htmlUrl="http://www.appinn.com"/>
</outline></body>
</opml>
读带有目录的就特别的麻烦,尤其是多层的或者无限层的就要用到递归。这里一般就只有两层,所以用for循环了。
界面写个textbox显示读取的数据
<Grid>
<TextBox Name="tx_1" TextWrapping="Wrap" Text="随便写写绑定用" />
</Grid>
mainwindow.cs下
public MainWindow()
{
InitializeComponent();
tx_1.Text = “”;
ReadOpml();
}
private void ReadOpml() {
string str = System.AppDomain.CurrentDomain.BaseDirectory;
if (File.Exists("default.opml"))
{
XmlDocument doc = new XmlDocument();
doc.Load("default.opml");
XmlNodeList otls = doc.GetElementsByTagName("outline");
for (int i = 0; i < otls.Count; i++)
{
if (otls[i].Attributes.GetNamedItem("type").InnerText == "NFgroup")
{
tx_1.Text += Environment.NewLine + otls[i].Attributes.GetNamedItem("text").InnerText + otls[i].Attributes.GetNamedItem("type").InnerText + Environment.NewLine;
continue;
}
else
{
tx_1.Text += otls[i].Attributes.GetNamedItem("text").InnerText + Environment.NewLine;
}
// createList(otls[i].Attributes.GetNamedItem("text").InnerText);
}
}
else
{
MessageBox.Show("没有opml文件,创建一个空的default.opml");
File.Create("default.opml");
}
}
当然把准备好的default.opml预先拷贝到自己的debug目录下。
效果图,但是这样是远远不够,的创建一个数据类型(list),来承接这些text,因为到时候要绑定到treeview中的。