的Visual C#2008,读取XML文件和填充一个ListView [英] Visual C# 2008, reading an XML file and populating a listView

查看:141
本文介绍了的Visual C#2008,读取XML文件和填充一个ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的终极目标......借此xml文件。

Here is my ultimate goal... to take this xml file..

<?xml version="1.0"?>
<Songs>
   <Song>
      <Name>Star Spangled Banner</Name>
      <Artist>Francis Scott Key</Artist>
      <Genre>Patriotic</Genre>      
   </Song>
   <Song>
      <Name>Blankity Blank Blank</Name>
      <Artist>Something Something</Artist>
      <Genre>Here here</Genre>
   </Song>
</Songs>

当我的应用程序启动时将其加载到一个列表视图组件。我也希望有诗经由两种或艺术家类型。

and load it into a list view component when my application starts up. I also want to have the "Songs" be separated into categories by either Artist or Genre.

我是新来的Visual C#,但我一直在编码一段时间,所以我真的只是找人点我在正确的方向在这里...

I'm new to Visual C#, but I've been coding for a while, so I'm really just looking for someone to point me in the right direction here...

在看谷歌,它看起来像人们似乎通过内容读取XML文件和循环来填充列表视图组件。这似乎对有所有这些数据,并用绑定源的东西的数量是不切实际的。它看起来像有应该是我的XML文件加载到数据集的方式,然后绑定源链接到数据集,然后终于在ListView链接到绑定源(或东西沿着这些线路)..虽然我'T似乎找到任何这些组件这将做到这一点的二传手的方法,我也上下打量没有成功这些组件的属性窗口。

Looking on google it looks like people seem to read XML files and loop through the contents to populate a list view component. Which seems impractical on the count that there are all of these "dataset" and "binding source" things available. It looks like there should be a way that I load the XML file into a data set, then link a binding source to the dataset, and then finally link the listview to the binding source (or something along these lines).. Though I can't seem to find any "setter" methods on any of these components which would do this, I've also looked up and down the "properties" window for these components with no success.

推荐答案

通过的LINQ to XML ,你可以做类似如下:

With Linq to XML, you can do something like the following:

XDocument loaded = XDocument.Load("myfile.xml");

var songs = from x in loaded.Descendants( "Song" )
select new
{
    Name = x.Descendants( "name" ).First().Value,
    Category = x.Descendants( "artist" ).First().Value,
    Genre = x.Descendants( "genre" ).First().Value,
}; //Returns an anonymous custom type

MyListView.DataSource = songs;  MyListView.DataBind();

现在你可以在你的的ListView 的模板,使用三个字段(从刚刚返回的匿名类型),您的信息就会显示出来。

Now you can use the three fields (from the anonymous type you just returned) in your ListView's template and your data will be displayed.

这篇关于的Visual C#2008,读取XML文件和填充一个ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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