我正在使用 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; }
}
我的 xml 文件
<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]