using System; using System.Linq; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; using System.ComponentModel; using System.Xml; using System.Xml.Linq; using System.Diagnostics; namespace LINQtoXSDWindowsApplication1 { public partial class Form1 : Form { /// <summary> /// Internal database manager /// </summary> private urn.books.books m_books_database; public Form1() { InitializeComponent(); //ask xml file to user openFileDialog1.Filter = "XML file (*.xml)|*.xml"; openFileDialog1.FileName = "books.xml"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { //opening database m_books_database = urn.books.books.Load(openFileDialog1.FileName); } else { m_books_database = null; } } /// <summary> /// Read the XML database and displays all books to debug and UI /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_list_books_Click(object sender, EventArgs e) { if (m_books_database == null) return; //query the full books list var list_books = from c in m_books_database.book select c; //write title and autor to debug console foreach (var b in list_books) Debug.WriteLine("Book : " + b.title + " from " + b.author); //databind the listbox (without managing render) listBox1.DataSource = list_books.ToList(); dataGridView1.DataSource = list_books.ToList(); } /// <summary> /// Displays all books with their author /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_list_books_and_authors_Click(object sender, EventArgs e) { if (m_books_database == null) return; var list_books_title = from c in m_books_database.book select c.title + " (" + c.author + ") Price:" + c.price.ToString() + "$"; listBox1.DataSource = list_books_title.ToList(); } /// <summary> /// Modify a book price and save result in a new xml file /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_modify_price_Click(object sender, EventArgs e) { if (m_books_database == null) return; var book001 = from c in m_books_database.book where c.id.CompareTo("bk001") == 0 select c; if (book001.First() != null) { book001.First().price = 1234; m_books_database.Save("books_price_modified.xml"); } } /// <summary> /// Delete a book from database /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_delete_book_Click(object sender, EventArgs e) { if (m_books_database == null) return; //select the book bk001 var book001 = from c in m_books_database.book where c.id.CompareTo("bk001") == 0 select c; //remove it foreach (var b in book001) m_books_database.book.Remove(b); //save as new file m_books_database.Save("books_deleted.xml"); } /// <summary> /// Create a new book /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_create_book_Click(object sender, EventArgs e) { if (m_books_database == null) return; //create the book instance urn.books.BookForm itemToAdd = new urn.books.BookForm(); itemToAdd.author = "Andrew Troelsen"; itemToAdd.genre="Programming"; itemToAdd.id="bk004"; itemToAdd.price=37.79f; itemToAdd.pub_date=Convert.ToDateTime("2007-12-07"); itemToAdd.title = "Pro C# 2008 and the .NET 3.5 Platform"; //add it to database m_books_database.book.Add(itemToAdd); //save the new database m_books_database.Save("books_create.xml"); } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)