Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
C#
<?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
Posted
Updated 17-May-13 11:35am
v9
Comments
Sergey Alexandrovich Kryukov 16-May-13 18:30pm    
The question about TreeView makes no sense unless you specify a fully-specified type name. There are several unrelated classes with the simple name TreeView.
You always need to tag the UI library or application type you use if UI is concerned.
—SA
Nnorss 16-May-13 18:40pm    
Ok Sergey i updated the question and specified the ui.
Sergey Alexandrovich Kryukov 16-May-13 19:13pm    
Where did you specify it? Tag it. Don't say "Treeview" or even "TreeView". Full name, please.
—SA
Nnorss 16-May-13 19:47pm    
Sorry but i don't understand what do you mean by "Full name"?
Sergey Alexandrovich Kryukov 16-May-13 19:58pm    
:Doh:... fully qualified. Called "FullName" in .NET. How did you write code so far? Did you ever look at someObject.GetType().FullName?
Not "TreeView", System.Window.Controls.TreeView, or one of the other names like that...
—SA

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
 
Share this answer
 
Comments
Nnorss 17-May-13 21:57pm    
All i have to say is thank you for the comment and I promess give me two months and i will be a good developper :D
Sergey Alexandrovich Kryukov 17-May-13 23:00pm    
You are welcome. Better not as soon but better. :-)
Are you accepting the answer formally (green button)?
—SA
Nnorss 18-May-13 0:20am    
For been honnest, I was looking for a little explication of how to extract the "text" value of the "outline" attribute from a OPML file... (I'm sorry I do have communication problems, maybe with the time my english will be much better)
Sergey Alexandrovich Kryukov 18-May-13 1:34am    
Sure, then please see Solution 3 I just added.
—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
 
Share this answer
 
Comments
Nnorss 18-May-13 2:55am    
Many thanks. I aslo founf this: http://support.microsoft.com/kb/317597
Sergey Alexandrovich Kryukov 18-May-13 5:43am    
Sure, good find.
Good luck, call again.
—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[^]
 
Share this answer
 
Comments
Nnorss 18-May-13 0:13am    
Ok thank you but it gives me an error:
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("//body/*"), treeView1.Nodes[0]);
}
private void LoadNode(XmlNodeList xmlNodes, TreeNode trvParent)
{

foreach (XmlNode xmlNode in xmlNodes)
{

TreeNode trnNewNode = new TreeNode(xmlNode.Attributes["outline"].Value); //error: the exeption nullReference is not managed...

if (xmlNode.Attributes["outline"] != null)

trnNewNode.Tag = xmlNode.Attributes["outline"].Value;

trnNewNode.Expand();

trvParent.Nodes.Add(trnNewNode);

LoadNode(xmlNode.ChildNodes, trnNewNode);

}

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900