如何将xml数据分组为几个月?

时间:2014-03-28 16:26:44

标签: c# xml windows-phone-8

根据问题,我怎样才能实现分组或者将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;

谢谢!

2 个答案:

答案 0 :(得分:1)

您需要的是LongListSelector控件。有关详情,请参阅http://www.geekchamp.com/articles/the-new-longlistselector-control-in-windows-phone-8-sdk-in-depth

答案 1 :(得分:1)

检查以下示例,按月和年分组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();
}
相关问题