我正在使用linq代码解析XML文件。这是我的代码。我想要绑定详细信息和图像列表。
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文件,我想获得带图像的名称和游戏
<?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>
我的输出就像
拉加
System.collection.generic.list'1[System.String]
System.collection.generic.list'1[System.String]
拉马
System.collection.generic.list'1[System.String]
System.collection.generic.list'1[System.String]
答案 0 :(得分:3)
你需要改变
s.Attribute("games").ToString()
x.Attribute("url").ToString()
为:
s.Attribute("games").Value.ToString()
x.Attribute("url").Value.ToString()
答案 1 :(得分:2)
修复XML标记结束后 - 关闭所有event_image标记并删除“1”(如果国际象棋图像如下)并删除代码的第1行和第3行,最后一行 - 序列化程序并调用它不是用过的。我得到了以下输出:
Raja
games="Cricket"
games="Foot Ball"
games="Volley Ball"
Rama
games="Chess"
games="Table Tennis"
games="Carrom"
-
<?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"/>
</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>
这是使用您的代码复制粘贴生成的,但我删除以下行:
XmlSerializer serializer = new XmlSerializer(typeof(Notchs)); //first line
Notchs notchs = (Notchs)serializer.Deserialize(xmlDoc.CreateReader()); //third line
NotchsList.ItemsSource = query.ToList(); //last line in your code snippet
答案 2 :(得分:1)
正如其他人已经提到过的,你的XML有一些拼写错误。修好并删除未使用的XmlSerializer
后,您的查询会产生有效的IEnumerable<Notch>
。一切都按预期工作。
但我强烈怀疑您希望在Titles
列表中拥有完整属性。其实你想拥有自己的价值观。所以你必须写Attribute(...).Value
并省略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
扩展方法来合并相关的标题和图片网址。但这只是为了获得更好的输出。数据已经正确。这是输出:
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
。这是一个“略微”简化但工作的版本:
<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>
这就是它的样子: