如何将xml数据分组到月份
本文关键字:数据 xml | 更新日期: 2023-09-27 17:54:45
根据问题,我如何实现分组或更确切地说,将xml数据分割成月份。这是我的XML代码。
<book_list total="10">
<book book_id="28174" book_name="So I Stole Your Bike" release_date="2014-03-25" Book_Store="Store A" />
<book book_id="13521" book_name="Why DID The Chicken Cross The Road?" release_date="2014-03-20" Book_Store="Store C" />
<book book_id="23122" book_name="Life without Breakfast" release_date="2014-03-18" Book_Store="Store B" />
<book book_id="13092" book_name="Caught Red Handed" release_date="2014-02-28" Book_Store="Store B" />
<book book_id="11765" book_name="Why did I wrote this book" release_date="2014-02-20" Book_Store="Store C" />
<book book_id="10133" book_name="If you can't read, then READ ME" release_date="2014-02-12" Book_Store="Store A" />
<book book_id="43291" book_name="Toe-may-toe or Thor-Mar-Thor" release_date="2014-02-2" Book_Store="Store A" />
<book book_id="82645" book_name="Look at me now" release_date="2014-01-27" Book_Store="Store B" />
<book book_id="00192" book_name="I can't believe you're still alive" release_date="2014-01-19" Book_Store="Store C" />
<book book_id="31452" book_name="The day pigs flew" release_date="2014-01-11" Book_Store="Store B" />
</book_list>
基本上我需要它以某种方式,每个月都显示那些出版的书。
March, 2014
Book Name : So I Stole Your Bike, Release Date : 2014-03-25, Book Store : Store A
Book Name : Why DID The Chicken Cross The Road?, Release Date : 2014-03-20, Book_Store : Store C
Book Name : Life without Breakfast Release Date, : 2014-03-18, Book_Store : Store B
February, 2014
Book Name : Caught Red Handed, Release Date : 2014-02-28, Book Store : Store B
Book Name : Why did I wrote this book, Release Date : 2014-02-20, Book_Store : Store C
Book Name : If you can't read, then READ ME, Release Date : 2014-02-12 Book_Store : Store A
Book Name : Toe-may-toe or Thor-Mar-Thor, Release Date : 2014-02-2, Book_Store : Store A
January, 2014
Book Name : Look at me now, Release Date : 2014-01-27, Book Store : Store B
Book Name : I can't believe you're still alive, Release Date : 2014-01-19, Book_Store : Store C
Book Name : The day pigs flew, Release Date : 2014-01-11, Book_Store : Store B
目前我就是这样检索XML文件的。但是,正如你所知道的,结果将显示一切,而不是将它们分成组。
var bookData = from book in _xdoc.Descendants("book")
select book;
ObservableCollection<BookList> Mybooks = new ObservableCollection<BookList>();
foreach (var book in bookData)
{
BookList mBook = new BookList();
mBook.Book_Name = match.Attribute("book_name").Value;
DateTime Release_Date = DateTime.Parse(match.Attribute("release_date").Value);
mBook.Book_Store = match.Attribute("book_store").Value;
Mybooks.Add(mBook);
}
BooksListBox.DataContext = Mybooks;
谢谢!
检查以下示例,按月份和年份对xml数据进行分组:
var group =
from book in _xdoc.Root.Elements("book")
let date = DateTime.Parse((string) book.Attribute("release_date"))
group book by new {date.Year, date.Month}
into g
select new
{
year = g.Key.Year,
month = g.Key.Month,
books = g.Select(o => new BookList
{
Book_Name = (string) o.Attribute("book_name"),
Release_Date = DateTime.Parse((string) o.Attribute("release_date")),
Book_Store = (string) o.Attribute("Book_Store")
})
};
foreach (var g in group)
{
Console.WriteLine("Group : {0}, {1}", g.year, g.month);
foreach (var book in g.books)
{
Console.WriteLine("Book Name: {0}. Release Date: {1}. Book Store: {2}",
book.Book_Name, book.Release_Date, book.Book_Store);
}
Console.WriteLine();
}
您需要的是一个LongListSelector控件。详见http://www.geekchamp.com/articles/the-new-longlistselector-control-in-windows-phone-8-sdk-in-depth