在XML列表中选择特定的5行

本文关键字:5行 选择 XML 列表 | 更新日期: 2023-09-27 17:51:24

我有一个包含10项的列表,如下面的XML所示。

<books_standing bestsellers="My Book" total="10">
  <book book_id="4" book_name="Book 4" copy_sold="41" position="1"/>
  <book book_id="14" book_name="Book 14" copy_sold="38" position="2"/>
  <book book_id="23" book_name="Book 23" copy_sold="33" position="3"/>
  <book book_id="1" book_name="Book 1" copy_sold="30" position="4"/>
  <book book_id="4" book_name="Book 4" copy_sold="28" position="5"/>
  <book book_id="25" book_name="Book 25" copy_sold="27" position="6"/>
  <book book_id="13" book_name="Book 13" copy_sold="24" position="7"/>
  <book book_id="7" book_name="My Book" copy_sold="20" position="8"/>
  <book book_id="8" book_name="Book 8" copy_sold="19" position="9"/>
  <book book_id="2" book_name="Book 2" copy_sold="18" position="10"/>
</book_standing>

我要做的是选择一个特定的行,然后显示上面和下面的两行。我想显示书名"我的书",目前在8的位置。我想在上面和下面的2个数据之间显示"我的书"。我的例子是位置,书名&;复制出售。

6  Book 25 27
7  Book 13 24
**8  My Book 20**
9  Book 8 19
10 Book 2 18

我能够提取数据,但不确定如何将注意力集中在我需要的那本书上。

class BookStanding
  {
    public int BookId { get; set; }
    public string BookName { get; set; }
    public string Position { get; set; }
    public string CopySold { get; set; }
  }
System.Xml.Linq.XDocument _xdoc = System.Xml.Linq.XDocument.Parse(responseAsString);
var bookData = from books in _xdoc.Descendants("book")
            select new
               {
                  book_id = books.Attribute("book_id").Value,
                  book_name = books.Attribute("book_name").Value,
                  position = books.Attribute("position").Value,
                  copy_sold = books.Attribute("copy_sold").Value
                };
            ObservableCollection<BookStanding> MyBookStanding = new ObservableCollection<BookStanding>();
            int bookIdx = 0;
            foreach (var books in bookData.Take(10))
            {
                BookStanding mBookStanding = new BookStanding();
                mBookStanding.BookId = Convert.ToInt32(books.book_id);
                mBookStanding.BookName = books.book_name;
                mBookStanding.Position = Convert.ToInt32(books.position);
                mBookStanding.CopySold = Convert.ToInt32(books.won);
                MyBookStanding.Add(mBookStanding);
                bookIdx++;
            }
            BooksListBox.DataContext = MyBookStanding;
            BooksGrid.Visibility = System.Windows.Visibility.Visible;

谢谢!

感谢@Nir Smadar在这个问题上帮助我。他给出的解决方案是在循环放入这些代码之后。

var BookPosition = MyBookStanding.Single(b => b.Book_Name == "My Book").Position;
var selectedBooks = MyBookStanding.Where(b => b.Position <= BookPosition + 2 && b.Position >= BookPosition - 2);
BooksListBox.ItemsSource = MyBookStanding;
BooksGrid.Visibility = System.Windows.Visibility.Visible;

在XML列表中选择特定的5行

加载条目后,您将得到一个名为" MyBookStanding "的图书集合。现在您需要找到所选书籍的位置:var bookPosition = MyBookStanding。Single(b => b. book_name == whateverBookName).position;

最后是5本书:var selectedBooks = MyBookStanding。Where(b => b position <= bookPosition + 2 || b position>= bookPosition - 2);

很抱歉没有格式化我的答案-我正在从我的单元格输入。