给我一个XML集合。 [英] Get me a collection from XML.

查看:55
本文介绍了给我一个XML集合。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,

我有一个xml(library.xml)文件作为数据源。我想在任何WPF控件中显示数据,其中数据应按作者和书籍进行分组和排序

Hello All,
I have a xml(library.xml) file as data source. I want to show the data in any WPF control, Where the data should be grouped and sorted by Authors and books

<?xml version="1.0" encoding="utf-8"?>
<Library>
<Book>
<Title>C# Basics</Title>
<Author>Dev</Author>
<Author>Sulfi</Author>
</Book>
<Book>
<Title>Java Core</Title>
<Author>Bjourne</Author>
</Book>
<Book>
<Title>Android Hands on</Title>
<Author>Dev</Author>
</Book>
<Book>
<Title>IOS First</Title>
<Author>Dev</Author>
<Author>Sulfi</Author>
</Book>
<Book>
<Title>Server 2008</Title>
<Author>SQ.Hell</Author>
</Book>
<Book>
<Title>C Path</Title>
<Author>Sulfi</Author>
</Book>
</Library>





任何人都可以帮我在控件上显示这些数据,如下所示





作者1

1.预订1

2. Book2

作者2

1.预订1

2.预订2

作者3

1. Boook1

作者4

1.预订1

2.预订2

3.预订3

推荐答案

嘿,



首先使用XMLReader读取XML,这样你就可以迭代了通过每一行,

,你可以在其中找到XML标签名称,使用它可以将值插入到相应的对象。

逻辑上你需要开发。

我给的是简短的代码。



Hey,

First read your XML using XMLReader, so you can iterate through each line,
where you can find XML tag name , using which you can insert the value to respective object.
Above logic you need to develope.
I am giving brief code.

public  class Author
    {
        public Author() { }

        public Author(String Strtext)
        {
            Strname = Strtext;
            LstBooks = new List<book>(); 
        }

        public void AddBook(String StrText)
        {
            Book ObjBook = new Book(StrText);
            LstBooks.Add(ObjBook);   
        }
        public string Strname;

        public List<book> LstBooks;
    }

    public class Book
    {
        public Book() { }

        public Book(String StrName)
        {
            Bookname = StrName;
        }
       
        public string Bookname;
    }


这是一个快速的示例解决方案。

它将所有数据保存在内存中(对于一个小的设置,没关系,否则使用数据库!)

文件../../ Library.xml只是显示的xml以上。

Here is a quick example solution.
It keeps all of the data in memory (for a small set, that's OK, otherwise use a database!)
The file "../../Library.xml" is just the xml shown above.
using System.Collections.Generic;
using System.Windows;
using System.Xml;
using System.Xml.Linq;

namespace WpfApplication3
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      Library = new Library();
      InitializeComponent();
    }
    public Library Library { get; private set; }
  }

  public class Library
  {
    private List<Book> Stacks = new List<Book>();
    private Dictionary<string, Author> Authors = new Dictionary<string, Author>();
    public Library()
    {
      using (XmlReader reader = new XmlTextReader("../../Library.xml"))
      {
        foreach (XElement bookElement in XDocument.Load(reader).Element("Library").Elements())
        {
          Book aBook = new Book(bookElement.Element("Title").Value);
          Stacks.Add(aBook);
          foreach (var authorElement in bookElement.Elements("Author"))
          {
            string authorName = authorElement.Value;
            Author author;
            if (!Authors.TryGetValue(authorName, out author))
            {
              author = new Author(authorName);
              Authors.Add(authorName, author);
            }
            aBook.Authors.Add(author);
            author.Books.Add(new IndexedItem<Book>(author.Books.Count + 1, aBook));
          }
        }
      }
    }
    public IEnumerable<Author> AllAuthors
    { get { return Authors.Values; } }
  }

  public class Book
  {
    public Book(string title)
    {
      Title = title;
      Authors = new List<Author>();
    }
    public string Title { get; private set; }
    public List<Author> Authors { get; private set; }
  }

  public class Author
  {
    public Author(string name)
    {
      Name = name;
      Books = new List<IndexedItem<Book>>();
    }
    public List<IndexedItem<Book>> Books { get; private set; }
    public string Name { get; private set; }
  }

  public class IndexedItem<T>
  {
    public IndexedItem(int index, T item)
    {
      Index = index;
      Item = item;
    }
    public int Index { get; private set; }
    public T Item { get; private set; }
  }
}





和WPF:



And the WPF:

<Window x:Class="WpfApplication3.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:local="clr-namespace:WpfApplication3"

        Title="MainWindow"

        Height="350"

        Width="525"

        DataContext="{Binding Path=Library, RelativeSource={x:Static RelativeSource.Self}}">
  <ListBox x:Name="topBox"

           ItemsSource="{Binding AllAuthors}">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <StackPanel Orientation="Vertical">
          <TextBlock x:Name="Name"

                     Text="{Binding Path=Name}"

                     HorizontalAlignment="Left"

                     VerticalAlignment="Center"

                     Margin="0,0,10,0" />
          <ListBox ItemsSource="{Binding Books}">
            <ListBox.ItemTemplate>
              <DataTemplate>
                <StackPanel Orientation="Horizontal">
                  <TextBlock x:Name="BookNumber"

                             Text="{Binding Path=Index}"

                             HorizontalAlignment="Left"

                             VerticalAlignment="Center"

                             Margin="10,0" />
                  <TextBlock x:Name="Title"

                             Text="{Binding Path=Item.Title}"

                             HorizontalAlignment="Left"

                             VerticalAlignment="Center" />
                </StackPanel>
              </DataTemplate>
            </ListBox.ItemTemplate>
          </ListBox>
        </StackPanel>
      </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>
</Window>





Styling the output is left as an exercise for the reader. ;)



Styling the output is left as an exercise for the reader. ;)


这篇关于给我一个XML集合。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆