RSS阅读器制作之将TreeView数据导出为XML数据
[XML] 纯文本查看 复制代码 //递归遍历节点内容,最关键的函数 private void parsenode(treenode tn) { ienumerator ie = tn.nodes.getenumerator(); while (ie.movenext()) { treenode ctn = (treenode) ie.current; //为最底层节点,即该节点为rss节点 if (ctn.getnodecount(true) == 0) { xmlline = getrsstext(ctn); sr.writeline(xmlline); }
using system;
using system.collections;
using system.io;
using system.windows.forms;
namespace treeviewexport
...{
/**//// <summary>
/// 将treeview中导出为xml数据
/// </summary>
public class treeviewtoxmlb
...{
//xml每行的内容
private string xmlline = "";
//用于写xml文件
private streamwriter sr;
/**//// <summary>
/// 构造函数
/// </summary>
public treeviewtoxmlb()
...{
}
/**//// <summary>
/// 将treeview中导出为xml数据
/// </summary>
/// <param name="tv">treeview控件</param>
/// <param name="filename">xml文件的保存路径</param>
public void exporttoxml(treeview tv, string filename)
...{
sr = new streamwriter(filename, false, system.text.encoding.utf8);
//写文件头部内容
//下面是生成rss的opml文件
sr.writeline("<?xml version="1.0" encoding="utf-8"?>");
sr.writeline("<opml version="1.0">");
sr.writeline("<head>");
sr.writeline("<title>rss</title>");
sr.writeline("</head>");
sr.writeline("<body>");
//遍历根节点
foreach (treenode node in tv.nodes)
...{
if (node.getnodecount(true) == 0)
...{
xmlline = getrsstext(node);
}
else
...{
xmlline = getdirectorytext(node);
}
sr.writeline(xmlline);
//递归遍历节点
parsenode(node);
}
//写文件尾部内容
sr.writeline("</body>");
sr.writeline("</opml>");
sr.close();
}
//递归遍历节点内容,最关键的函数
private void parsenode(treenode tn)
...{
ienumerator ie = tn.nodes.getenumerator();
while (ie.movenext())
...{
treenode ctn = (treenode) ie.current;
//为最底层节点,即该节点为rss节点
if (ctn.getnodecount(true) == 0)
...{
xmlline = getrsstext(ctn);
sr.writeline(xmlline);
}
else//不是最底层节点,即该节点为目录节点
...{
xmlline = getdirectorytext(ctn);
sr.writeline(xmlline);
}
//如果还有子节点则继续遍历
if (ctn.getnodecount(true) > 0)
...{
parsenode(ctn);
}
}
sr.writeline("</outline>");
}
//成生rss节点的xml文本行
private string getrsstext(treenode node)
...{
//根据node属性生成xml文本
string rsstext = "<outline type="rss" text="" + node.text + "" xmlurl="url"/>";
return rsstext;
}
//成生目录节点的xml文本行
private string getdirectorytext(treenode node)
...{
//根据node属性生成xml文本
string directorytext = "<outline text="" + node.text + "">";
return directorytext;
}
}
}
|