Context: XAML, WPF, C#.
There is some XML data. (see below).
There is a ListBox that should display the InnerText of subNodes of nodes (because there are others same level as node with as subnode)
I obtained XmlNodesList with news= "getTagsbyname("item");
I've set the ItemsSource for the Listbox to news
But I want displayed ONLY the title, not all the data inside the
So I've tried with DisplayMemberPath= "title";
Unfortunately this is not working, because there is no "news.title" property. Instead the data needed is in news.ChildNodes[0].InnerText
So I've tried with DisplayMemberPath = "ChildNodes[0].InnerText". Obviously, this in not working either.
XmlDocument rss = new XmlDocument();
rss.Load("c:\rss.xml");
XmlElement rc = rss.DocumentElement;
XmlNodeList news = rc.GetElementsByTagName("item");
listbox1.ItemsSource = news;
listbox1.DisplayMemberPath = "ChildNodes[0].InnerText";
Because I didn't know how to solve this, I've switched to XAML and solved the issue with XmlDataProvider, DataTemplate and Xpath:
<XmlDataProvider x:Key="titles" Source="C:\rss.xml" XPath="/rss/channel/item" />
<ListBox Name="listbox1" SelectionChanged="listbox1_SelectionChanged" ItemsSource=" {Binding Source={StaticResource titles}}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=title}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
My question is:
How to accomplish the same thing in C#, not in XAML ?
Because I want the rss link to be specified in a textBox (by user) like rss.Load (textBox1.Text) instead of saving the .xml file locally first.
My problem is the "news" is a List and the data is not a property or attribute but a property of a member i.e. news.ChildNodes[0].InnerText. And the syntax accessing this - I dont know how to pass it in a string for ItemsSource or DisplayMemberPath.
I hope I was clear and specific describing my problem.
Thanks in advance for your valuable suggestions.
Here is the XML model.
<rss>
<channel>
<author>
<title>qwer</title>
<image/>
<link>www</link>
</author>
<item>
<title>header1</title>
<description>abcd</description>
<category>1</category>
</item>
<item>
<title>header2</title>
<description>efgh</description>
<category>2</category>
</item>
</channel>
</rss>