简体   繁体   English

用C#而不是XAML以编程方式处理xmlnodeslist

[英]process xmlnodeslist programatically in c# instead of XAML

Context: XAML, WPF, C#. 上下文:XAML,WPF,C#。

There is some XML data. 有一些XML数据。 (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) 有一个列表框应显示节点子节点的InnerText(因为还有其他与节点相同级别的子节点)

I obtained XmlNodesList with news= "getTagsbyname("item"); 我获得了XmlNodesList,其中news =“ getTagsbyname(” item“);

I've set the ItemsSource for the Listbox to news 我已将列表框的ItemsSource设置为news

But I want displayed ONLY the title, not all the data inside the 但我只想显示标题,而不是其中的所有数据

So I've tried with DisplayMemberPath= "title"; 因此,我尝试使用DisplayMemberPath =“ title”;

Unfortunately this is not working, because there is no "news.title" property. 不幸的是,这不起作用,因为没有“ news.title”属性。 Instead the data needed is in news.ChildNodes[0].InnerText 相反,所需的数据在news.ChildNodes [0] .InnerText中

So I've tried with DisplayMemberPath = "ChildNodes[0].InnerText". 因此,我尝试使用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: 因为我不知道如何解决此问题,所以我切换到XAML并使用XmlDataProvider,DataTemplate和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 ? 如何在C#中而不是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. 因为我希望在rss.Load(textBox1.Text)之类的textBox中(由用户)指定rss链接,而不是先在本地保存.xml文件。

My problem is the "news" is a List and the data is not a property or attribute but a property of a member ie news.ChildNodes[0].InnerText. 我的问题是“新闻”是一个列表,数据不是属性或属性,而是成员的属性,即news.ChildNodes [0] .InnerText。 And the syntax accessing this - I dont know how to pass it in a string for ItemsSource or DisplayMemberPath. 以及访问该语法的语法-我不知道如何在ItemsSource或DisplayMemberPath的字符串中传递它。

I hope I was clear and specific describing my problem. 我希望我清楚明确地描述了我的问题。

Thanks in advance for your valuable suggestions. 预先感谢您的宝贵建议。

Here is the XML model. 这是XML模型。

<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>

You can do it with LINQ to XML . 您可以使用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: 并阅读您的Xml文件:

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) 然后填充您的listView (使用listView而不是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. 这里我们将您的rssItem存储在item的Tag属性中。因此,如果您需要访问该项目,则可以使用SelectedItem.Tag属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM