gpt4 book ai didi

c# - 获取列表

转载 作者:行者123 更新时间:2023-12-02 09:44:43 25 4
gpt4 key购买 nike

我正在使用 linq 代码解析 XML 文件。这是我的代码。我想要绑定(bind)详细信息和图像列表。

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]

最佳答案

你需要改变

s.Attribute("games").ToString()
x.Attribute("url").ToString()

至:

s.Attribute("games").Value.ToString()
x.Attribute("url").Value.ToString()

关于c# - 获取列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15194755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com