0

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>
acadea
  • 37
  • 1
  • 5

1 Answers1

0

You can do it with LINQ to XML. I guess it would be better if you define a class like this:

public class RssItem
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string Category { get; set; }
}

And read your Xml file:

XDocument xDoc = XDocument.Load(@"c:\rss.xml");

var items = (from x in xDoc.Descendants("item")
                select new RssItem
                {
                   Title = (string) x.Element("title"),
                   Description = (string) x.Element("description"),
                   Category = (string) x.Element("category")
                }).ToList();

Then populate your listView (use listView instead of listBox)

foreach (var rssItem in items)
{
     ListViewItem item  = new ListViewItem();
     item.Text = rssItem.Title;
     item.Tag = rssItem;
     listView1.Items.Add(item);
 }

Here we are storing your rssItem into item's Tag property.So if you need access that item you can use SelectedItem.Tag property.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • before posting the question, I've search a lot here and elsewhere. most of the answers to this kind of issues regarding xml and databinding were : "you can do it with LINQ". sorry, I forgot to mention : I don't want to do it with LINQ. I don't want to obtain a quick solution using another approach. I just want to understand how to do it with in the classic manner. also, I've read your answer and I've understood it. thank your for your answer. – acadea Jan 05 '14 at 22:08