我目前有一个URL请求带回XML数据。我将该数据存储在读取的文档中,并查找某些属性之间的信息,并将这些值分配给我指定的变量。我的wpf DataGrid叫做Movie_DataGrid。如何将这些数据提供给DataGrid会有任何帮助。
- 编辑 -
我用一种新的方式更新了我的代码,我试图得到我的结果。当单步执行代码的每一步时,XML存储正常,并且Retrivalinfo类和Retrievalinfo convertedMovie = new Retrievalinfo()之间的所有标记属性都相同,但是此方法的应用程序错误。
我的新问题是,属性中的值未被抓取和存储。我还提供了一些我会得到的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>
// 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;
// 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("http://www.omdbapi.com/?i=" + titleID + "&r=XML");
// Creates a XML Noedlist to store the values that are going to be associated with the given attribute tag.
XmlNodeList movieList = doc.GetElementsByTagName("movie");
var movie = movieList.Item(0);
Retrievalinfo convertedMovie = new Retrievalinfo()
{
title = movie.Attributes["title"].ToString(),
actors = movie.Attributes["actors"].ToString().Split(',').ToList(),
genre = movie.Attributes["genre"].ToString(),
rated = movie.Attributes["rated"].ToString(),
imdbRating = movie.Attributes["imbdRating"].ToString(),
released = movie.Attributes["released"].ToString(),
runtime = movie.Attributes["runtime"].ToString(),
};
List<Retrievalinfo> gridInfo = new List<Retrievalinfo>();
Movie_DataGrid.ItemsSource = gridInfo;
以下是我希望在DataGrid中显示每个变量的类。
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;
public List<string> actors;
public string genre;
public string rated;
public string imdbRating;
public string released;
public string runtime;
}
}
答案 0 :(得分:2)
我虽然写了一个冗长的aswer但是,这里有一个快速的样本供你自己使用,作为参考并自己弄清楚细节。 MVVM不包括在内:D
希望它有所帮助。
<强>代码隐藏强>
namespace MyMovies
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
Movies = new ObservableCollection<Movie>()
{
new Movie("Lock, Stock and Two Smoking Barrels", 4),
new Movie("Life of Brian", 5),
};
var addMovieCommand = new RoutedUICommand();
CommandManager.RegisterClassCommandBinding(typeof(Window),
new CommandBinding(
addMovieCommand,
(sender, args) => AddMovie(),
(sender, args) => args.CanExecute = true));
AddMovieCommand = addMovieCommand;
}
public ObservableCollection<Movie> Movies { get; set; }
public ICommand AddMovieCommand { get; set; }
private void AddMovie()
{
Movies.Add(new Movie(Guid.NewGuid().ToString(), 3));
}
}
public class Movie
{
public Movie(string name, int stars)
{
Name = name;
Stars = stars;
}
public string Name { get; set; }
public int Stars { get; set; }
}
}
<强> XAML 强>
<Window x:Class="MyMovies.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<DataGrid
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{Binding Movies}">
</DataGrid>
<Button Content="Add movie" Command="{Binding AddMovieCommand}" />
</StackPanel>
</Grid>
</Window>
哪个给你
答案 1 :(得分:0)
如果我正确理解你的问题,你需要一些:
ObservableCollection<RetrievalInfo>
用于存储检索到的数据我很乐意为您不确定如何实施的任何或所有部分提供样品。
答案 2 :(得分:0)
您可以使用以下博客中提供的代码段读取xml到对象列表
博客链接:
http://danielwylie.me/blog/2010/04/c-convert-xml-to-an-object-or-list-of-an-object
您可以使用以下代码段
指定dataGrid的ItemSourceMovie_DataGrid.ItemsSource = list;
//here list object from public static List<T> XmlToObjectList<T>(string xml, string nodePath) method