【发布时间】:2014-03-03 14:09:10
【问题描述】:
我已经阅读了多个答案,但根本无法解决我的问题。我有一个从 Web 请求接收 XML 的程序,在该程序中我只获取该 XML 中的特定属性。单步执行时,我可以看到一切正常,检索获取正确的 XML,排序并获取正确的标签,然后将每个标签的内容分配给我在另一个类中的关联变量。此外,当单步执行时,“gridInfo”会显示我想要显示的每个值的列表。
我无法将列表绑定到 DataGrid。如您所见,我创建了一个新的 ObserableCollection 类,其中存储了每个变量并调用该对象。我创建了该类的列表并将 XML 中的值存储到该列表中。有人可以帮忙吗?
此外,当您查看我读到的 XML 时,您会注意到它不是正常格式,所有内容都包含在其自己的开始和结束标记中,这就是为什么我必须使用 DocumentElement 以及 GetElementsByTagName
代码隐藏:
// This action will seach the IMDb API for the associated infromation for the IMDBID that is tagged with the title you chose in the ListBox.
private void Movie_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ // Grabs the IMDBID associated to the movie title selected to be used with the second API request.
var p = Movie_List.SelectedIndex;
string titleID = structholder[p].IMDBID;
string newurl = "http://www.omdbapi.com/?i=" + titleID + "&r=XML";
// Prepares 2nd API URL request to get data for chosen title.
// Creates a XML Document to store the xml data that was sent back by the API.
XmlDocument doc = new XmlDocument();
doc.Load(newurl);
// Creates a XML Noedlist to store the values that are going to be associated with the given attribute tag.
XmlNodeList movieList = doc.DocumentElement.GetElementsByTagName("movie");// GetElementsByTagName("root");
ObservableCollection<Retrievalinfo> test = new ObservableCollection<Retrievalinfo>();
List<Retrievalinfo> gridInfo = new List<Retrievalinfo>();
foreach (XmlNode node in movieList)
{
gridInfo.Add(new Retrievalinfo(){
title = node.Attributes["title"].Value.ToString(),
actors = node.Attributes["actors"].Value.Split(',').ToList(),
genre = node.Attributes["genre"].Value.ToString(),
rated = node.Attributes["rated"].Value.ToString(),
imdbRating = node.Attributes["imdbRating"].Value.ToString(),
released = node.Attributes["released"].Value.ToString(),
runtime = node.Attributes["runtime"].Value.ToString(),
});
}
Movie_DataGrid.ItemsSource = Retrievalinfo;
}
Retrivealinfo 类:
namespace WpfApplication3
{
public class Retrievalinfo
{
public Retrievalinfo()
{
actors = new List<string>();
}
//Creating a list of info objects that will store all returned data for selected title.
public string title { get; set; }
public List<string> actors { get; set; }
public string genre { get; set; }
public string rated { get; set; }
public string imdbRating { get; set; }
public string released { get; set; }
public string runtime { get; set; }
}
}
XAML:
<ListBox x:Name="Movie_List" ItemsSource="{Binding listInfo}" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="287" Margin="20,107,0,0" VerticalAlignment="Top" Width="198" SelectionChanged="Movie_List_SelectionChanged" />
<Label Grid.ColumnSpan="2" Content="Movie List" HorizontalAlignment="Left" Height="30" Margin="70,72,0,0" VerticalAlignment="Top" Width="99" FontSize="16" FontFamily="Cooper Black" />
<DataGrid x:Name="Movie_DataGrid" Grid.ColumnSpan="2" HorizontalAlignment="Left" Margin="232,107,0,0" VerticalAlignment="Top" Height="198" Width="497" AutoGenerateColumns="True" ItemsSource="{Binding gridInfo}">
<DataGrid.Columns>
<DataGridTextColumn Header="Title" Binding="{Binding Path=title}"/>
<DataGridTextColumn Header="Main Actor 1" Binding="{Binding Path=actors[0]}"/>
<DataGridTextColumn Header="Main Actor 2" Binding="{Binding Path=actors[1]}"/>
<DataGridTextColumn Header="Main Actor 3" Binding="{Binding Path=actor[2]}"/>
<DataGridTextColumn Header="Genre" Binding="{Binding Path=genre}"/>
<DataGridTextColumn Header="Rated" Binding="{Binding Path=rated}"/>
<DataGridTextColumn Header="IMDB Rating" Binding="{Binding Path=imdbRating}"/>
<DataGridTextColumn Header="Released" Binding="{Binding Path=released}"/>
<DataGridTextColumn Header="Runtime" Binding="{Binding Path=runtime}"/>
</DataGrid.Columns>
</DataGrid>
我读入的 XML:
<root response="True">
<movie title="Up in the Air" year="2009" rated="R" released="23 Dec 2009" runtime="109
min" genre="Drama, Romance" director="Jason Reitman" writer="Walter Kirn (novel), Jason
Reitman (screenplay), Sheldon Turner (screenplay)" actors="George Clooney, Vera Farmiga,
Anna Kendrick, Jason Bateman" plot="With a job that has him traveling around the country
firing people, Ryan Bingham leads an empty life out of a suitcase, until his company
does the unexpected: ground him." language="English" country="USA" awards="Nominated for
6 Oscars. Another 64 wins & 66 nominations."poster="http://ia.mediaimdb.com/images/M/MV5BMTI3MzYxMTA4NF5BMl5BanBnXkFtZTcwMD
E4ODg3Mg@@._V1_SX300.jpg" metascore="83" imdbRating="7.5" imdbVotes="215,961" imdbID="tt1193138" type="movie"/>
</root>
【问题讨论】:
-
Movie_DataGrid.ItemsSource = 检索信息; Retrievalinfo 是一个类而不是一个集合。我很惊讶编译。试试 Movie_DataGrid.ItemsSource = gridInfo;
标签: c# xml wpf binding datagrid