Click here to Skip to main content
15,886,035 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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
<?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>


Could any one please help me to show this data on a control as given below


Author1
1. Book1
2. Book2
Author2
1. Book1
2. Book2
Author3
1. Boook1
Author4
1. Book1
2. Book2
3. Book3
Posted

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.

C#
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;
    }
 
Share this answer
 
v2
Comments
Salam6331 19-Sep-13 23:34pm    
Hello Kajal204/ Matt T Heffron.
I have managed to create a list by the above classes, As you can see the prototype given above, I have to show its as an Ordered List. How can attach these item numbers in a control like ListBox,is it need to add another field in author to store the book number?
Kajal204 20-Sep-13 5:42am    
Hello,

Could you specify more details about your tasks?

My Doubts-
1) Do u want to show above Author List as an Ordered List(HTML Tag i.e. <ol> tag) ?
2) Do u want to show in Listbox which contains Author or Book or BOTH,is it?

For unique book name you should need Book number,if u r using Book in Listbox.
Salam6331 20-Sep-13 5:48am    
Hello Kajal204
I want to Show this in Any WPF control,not in HTML.
Where the list ill be like
Author1
1. Book1
2. Book2
Author2
1. Book1
2. Book2
Author3
1. Boook1
Author4
1. Book1
2. Book2
3. Book3
Salam6331 20-Sep-13 5:50am    
both Authors and their books will come in alphabetically sorted and the book name will be followed by a number bullet.
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.
C#
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; }
  }
}


And the WPF:
XML
<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. ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900