获取清单 [英] Getting the list
问题描述
我正在使用linq代码解析XML文件.这是我的代码.我想将绑定详细信息和图像列出.
I am using linq code parsing XML file.This is my code.I want bind detail and image are list.
XmlSerializer serializer = new XmlSerializer(typeof(Notchs));
XDocument xmlDoc = XDocument.Parse(dataInXmlFile);
Notchs notchs = (Notchs)serializer.Deserialize(xmlDoc.CreateReader());
var query = from l in xmlDoc.Descendants("Person")
select new Notch
{
name = (string)l.Attribute("name").Value,
Titles = l.Element("Details").Elements("detail")
.Select(s => s.Attribute("games").ToString())
.ToList(),
Image = l.Element("Details").Elements("detail").Elements("event_image").Elements("image")
.Select(x => x.Attribute("url").ToString()).ToList()
};
foreach (var result in query)
{
Console.WriteLine(result.name);
foreach (var detail in result.Titles)
{
Console.WriteLine(detail);
}
}
NotchsList.ItemsSource = query.ToList();
这是我正在使用XML文件,我想使用图像获取名称和游戏
This is i am using XML file and i want to get name and games with image
<?xml version="1.0" encoding="utf-8"?>
<root>
<Peoples>
<Person name="Raja">
<Details>
<detail games="Cricket">
<event_image>
<image url="http://Cricket.jpeg"/>
<event_image>
</detail>
<detail games="Foot Ball">
<event_image>
<image url="http://FootBall.jpeg"/>
<event_image>
</detail>
<detail games="Volley Ball">
<event_image>
<image url="http://.Volley.jpeg3"/>
<event_image>
</detail>
</Details>
</Person>
<Person name="Rama">
<Details>
<detail games="Chess">
<event_image>
<image url="http://Chess.jpeg"/1>
<event_image>
</detail>
<detail games="Table Tennis">
<event_image>
<image url="http://TTennis.jpeg"/>
<event_image>
</detail>
<detail games="Carrom">
<event_image>
<image url="http://Carrom.jpeg"/>
<event_image>
</detail>
</Details>
</Person>
</Peoples>
</root>
我的代码喜欢
public class Notch
{
[XmlAttribute("name")]
public string name { get; set; }
[XmlAttribute("games")]
public List<string> Titles { get; set; }
[XmlAttribute("url")]
public List<string> Image { get; set; }
}
[XmlRoot("root")]
public class Notchs
{
[XmlArray("Peoples")]
[XmlArrayItem("Person")]
[XmlArrayItem("Details")]
public ObservableCollection<Notch> Collection { get; set; }
}
我的xaml文件
<ListBox x:Name="NotchsList"
SelectionChanged="NotchsList_SelectionChanged" Margin="0,-5.25,22,0" Grid.Row="1" HorizontalAlignment="Right" Width="768" Grid.Column="1" Grid.RowSpan="3" d:LayoutOverrides="VerticalMargin">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,0" Orientation="Vertical" Grid.ColumnSpan="2"
Grid.Column="0"
Height="160"
VerticalAlignment="Top">
<StackPanel Background="#eb2427" Orientation="Vertical">
<TextBlock Grid.Row="1" FontFamily="Calibri" FontSize="34" FontWeight="Bold" FontStyle="Normal" Margin="10,0,0,0"
Text="{Binding name}"
/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Grid.Row="1" FontFamily="Calibri" FontSize="32" Foreground="#a7a9ac"
Text="{Binding games}" ScrollViewer.HorizontalScrollBarVisibility="Visible"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Image Grid.Row="1" Source="{Binding Image}" VerticalAlignment="top" />
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我得到了类似的输出
Raja
System.collection.generic.list'1[System.String]
System.collection.generic.list'1[System.String]
Rama
System.collection.generic.list'1[System.String]
System.collection.generic.list'1[System.String]
推荐答案
正如其他人已经提到的那样,您的XML有一些错别字.解决该问题并删除未使用的XmlSerializer
后,您的查询将产生一个有效的IEnumerable<Notch>
.一切都按预期进行.
As others already mentioned your XML has a few typos. After fixing that and removing the unused XmlSerializer
your query produces a valid IEnumerable<Notch>
. Everything works as expected.
但是我非常怀疑您是否想在Titles
列表中拥有完整的属性.实际上,您想拥有他们的价值观.因此,您必须编写Attribute(...).Value
并省略ToString()
:
But I strongly doubt that you want to have the full attributes in your Titles
list. Actually you want to have their values. So you have to write Attribute(...).Value
and omit ToString()
:
var query = from l in xmlDoc.Descendants("Person")
select new Notch
{
name = (string)l.Attribute("name").Value,
Titles = l.Element("Details").Elements("detail")
.Select(s => s.Attribute("games").Value)
.ToList(),
Image = l.Element("Details").Elements("detail")
.Elements("event_image").Elements("image")
.Select(x => x.Attribute("url").Value).ToList()
};
foreach (var result in query)
{
Console.WriteLine(result.name);
foreach (var detail in result.Titles.Zip(result.Image, (st, si) => string.Format("{0} {1}", st, si)))
{
Console.WriteLine(detail);
}
}
请注意,我使用了 Zip
扩展方法来组合相关标题和图片网址.但这只是为了获得更好的输出.数据已经正确.这是输出:
Note that I used the Zip
extension method to combine related titles and image urls. But this is only for a better output. The data is already correct. This is the output:
Raja
Cricket http://Cricket.jpeg
Foot Ball http://FootBall.jpeg
Volley Ball http://.Volley.jpeg3
Rama
Chess http://Chess.jpeg
Table Tennis http://TTennis.jpeg
Carrom http://Carrom.jpeg
但是,您的数据不会显示在列表框中.您绑定到games
而不是Titles
,并使用TextBlock
显示一个列表,该列表产生您提到的输出.它也应该是ListBox
.这是一个略微"简化但有效的版本:
However your data will not show up in your listbox. You bind to games
instead of Titles
and you use TextBlock
to show a list which produces the output you mentioned. It should also be a ListBox
. Here is a "slightly" simplified but working version:
<ListBox x:Name="NotchsList">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding name}" />
<StackPanel Orientation="Horizontal">
<ListBox ItemsSource="{Binding Titles}" Width="200" />
<ListBox ItemsSource="{Binding Image}" Width="200" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是它的外观:
这篇关于获取清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!