从OPML(XML)文件填充TreeView [英] Populating TreeView from OPML(XML) file

查看:80
本文介绍了从OPML(XML)文件填充TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个带有textBox和treeView的窗体,textBox包含一个OPML文件的位置(c://documents/file.opml)。如何解析文件并将一些提取的文本转换为System.Windows.Forms.TreeView?

以下是文件内容的示例:

 < ?xml version =   1.0 encoding =   utf-8>  
< opml version = 1.1 >
<头>
< title>我的订阅< / 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 = 科学信号,信号转导知识环境 language = < span class =code-string> 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édugolf 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édesrallyes type = RSS />
< / outline >
< / outline >
< outline text = Catégorie2 >
< outline text = economy >
< outline text = 商业日报 xmlUrl = http://downloads.bbc.co.uk/podcasts/worldservice/ bizdaily / rss.xml htmlUrl = http://www.bbc.co.uk/programmes / p002vsxs description = 检查大商业日报面临着全球经济面临的问题,揭开了货币世界的神秘面纱。从航空和汽车等大型行业到规模最小的初创企业,商业日报都提出了有关自由贸易,技术和投资的重大问题。还有管理和营销趋势的分析,以及商业术语的真正含义 - 以及来自世界各地的商业新闻报道,通过BBC的全球记者网络。 type = RSS />
< outline text = Business&#38;经济保障| PBS NewsHour播客| PBS xmlUrl = http://www.pbs.org/newshour/rss/podcast_business.xml htmlUrl = http://www.pbs.org/newshour/topic/business/ description = 有关Business&#38的最新消息,分析和报告;来自PBS NewsHour及其网站的经济,通过访谈,背景报告和更新定期更新Feed,以便将新闻放在更大的背景中。 type = RSS />
< / 大纲 >
< outline text = politic >
< outline text = 来自
CBC新闻网的Evan Solomon的电力与政治(精彩集锦)
xmlUrl = http://www.cbc.ca/podcasting/includes/powerandpolitics.xml htmlUrl = http://www.cbc.ca/podcasting description = type = RSS />
< outline text = 政治周刊 xmlUrl = http://www.guardian.co.uk/politics/series/politicsweekly/podcast.xml htmlUrl = http://www.guardian.co.uk/audio description = 来自Guardian and Observer的顶级记者和评论员的政治分析 type = RSS />
< / 大纲 >
< / 概要 <跨度class =code-keyword>>
< / body >
< / opml >



以下是我要展示的内容:

< pre lang =text> - 分类1
-sience
Sience Signaling
-sport
L'Equipe.fr Actu Golf
L'Equipe.fr Actu Rally
- 分类2
-经济
商业日报
商业&#38;经济保障| PBS NewsHour播客| PBS
-politic
Evan Solomon的权力和政治来自......
政治周刊

解决方案

好的,它无处可去。您只需要:

http:// msdn.microsoft.com/en-us/library/system.windows.forms.treeview.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.aspx [ ^ ],

http://msdn.microsoft.com/en- us / library / system.windows.forms.treenode.nodes.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.treenodecollection.aspx [ ^ ]。



注意最后一个链接,几个 System.Windows.Forms.TreeNodeCollection.Add 方法。这是您最终需要填充树视图的所有内容。这就是全部。



我无法想象你在做开发。我觉得你需要回过头来学习基础知识:什么是类型,实例(对象),变量,成员,静态与实例的所有成员,然后是OOP,一切。只有这样,您才能回到UI开发或其他高级主题。或许你有一些沟通问题,缺乏基本术语的知识,我不知道。你需要做些什么...



-SA


Nnorss写道:

我正在寻找如何从OPML文件中提取outline属性的text值的一点解释

当然,我请参阅。



这只是一个XML文件;和.NET Framework的FCL为XML解析提供了足够的可能性。这是我对他们的简短概述:



  1. 使用 System.Xml.XmlDocument class 。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。
  2. 使用类 System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。
  3. 使用类 System.Xml.Linq.XDocument ;这是类似于 XmlDocument 的最合适的方式,支持LINQ to XML Programming。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ],http://msdn.microsoft.com/en-us/library/bb387063.aspx [ ^ ]。





祝你好运,

-SA


您必须将OPML文件读作XmlDocument并且通读节点。然后将每个节点添加到TreeNodes。代码可在以下链接中找到。



http://channel9.msdn.com/Forums/TechOff/180598-Loading-OPML-into-a-TreeView-C [ ^ ]


Hello, I have a windows form with a textBox and a treeView, the textBox contains the location of an OPML file("c://documents/file.opml"). How can I parse the file and transform some extracted text into System.Windows.Forms.TreeView?
Here is an exemple of the content of the file:

<?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 &#38; 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 &#38; 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>


Here is what i want to show:

-Categorie1
   -sience
      Sience Signaling
   -sport
      L'Equipe.fr Actu Golf
      L'Equipe.fr Actu Rally
-Categorie2
   -economy
      Business Daily
      Business &#38; Economy Coverage | PBS NewsHour Podcast | PBS
   -politic
      Power and Politics with Evan Solomon from the...
      Politics Weekly

解决方案

OK, it goes nowhere. All you need is this:
http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.nodes.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenodecollection.aspx[^].

Pay attention for the last link, the several System.Windows.Forms.TreeNodeCollection.Add method. This is all you ultimately need to populate the tree view. That''s all.

I fail to imagine you doing development. I feel you need to get back and learn the very basics: what is type, instance (object), variable, member, static vs. instance for all members, then OOP, everything. Only then you can get back to UI development or other advanced topic. Or maybe you have some communication problems, lack of knowledge of basic terminology, I don''t know. You need to do something about it…

—SA


Nnorss wrote:

I was looking for a little explication of how to extract the "text" value of the "outline" attribute from a OPML file

Sure, I see.

This is merely an XML file; and .NET Framework''s FCL offers enough possibilities for XML parsing. This is my short overview of them:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].



Good luck,

—SA


You have to read the OPML file as XmlDocument and read through nodes. Then add each node to the TreeNodes. Code is available on following link.

http://channel9.msdn.com/Forums/TechOff/180598-Loading-OPML-into-a-TreeView-C[^]


这篇关于从OPML(XML)文件填充TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆