public List<Book> GetBooks(string searchQuery, Bookenum tempAttributes) { try { return controller.GetBookFromXmlParser(searchQuery, tempAttributes); } catch (Exception e) { Console.Write(e.StackTrace); return null; } }
/// <summary> /// Returns a list of books from the XmlParser class. /// </summary> /// <param name="searchQuery"></param> /// <param name="tempAttributes"></param> /// <returns></returns> public List<Book> GetBookFromXmlParser(string searchQuery, Bookenum tempAttributes) { return xmlParser.ParseXml(searchQuery, tempAttributes); }
/// <summary> /// This method takes the inparameters searchQuery and tempAttributes to find the correct books and return /// them in a list of books. /// To search the xml-file an XmlTestReader is used. It locates the xml-file /// and reads it into a dataset. /// /// The method then loops through the table and rows of the dataset to find the right match from the searchQuery. /// The method uses a switch case statement to check which attributes of the book the user has choosen to search for. /// Depending on the inparameter of the attribute, which is found in the enum bookAttributes, the method choose a case. /// The case finds the row of the dataset where the field equals the attribute. /// /// Thereafter it checks the value of the field, and if it contains the searchQuery for a match. /// For the user to be able to search independent of capital or lower case letters the searchQuery and the /// value of the matched field both uses the native method ToUpper() to match the searchresult with the query. /// If it's a match the temporary bookobject get's the attributes of the matched field and attribute. /// /// The temporary book is finally added to the list of books and the switch break and the method returns the list. /// </summary> /// <param name="searchQuery"></param> /// <param name="tempAttributes"></param> /// <returns></returns> public List<Book> ParseXml(string searchQuery, Bookenum tempAttributes) { searchQuery = searchQuery.ToUpper(); List<Book> listOfBooks = new List<Book>(); XmlTextReader xmlReader = new XmlTextReader(libApp.Server.MapPath("~/XML/books.xml")); DataSet dataSet = new DataSet(); dataSet.ReadXml(xmlReader); foreach (DataTable table in dataSet.Tables) { foreach (DataRow row in table.Rows) { Book tempBook = new Book(); switch (tempAttributes) { case Bookenum.Author: if (row.Field<String>("author").ToUpper().Contains(searchQuery)) { tempBook.Title = row.Field<String>("title"); tempBook.Author = row.Field<String>("author"); tempBook.Description = row.Field<String>("description"); tempBook.Genre = row.Field<String>("genre"); tempBook.Publish_date = row.Field<String>("publish_date"); tempBook.Id = row.Field<String>("id"); //Changing to us culture so the price can be parsed correctly to decimal. System.Globalization.CultureInfo usCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); String tempString = row.Field<String>("price"); decimal d = Decimal.Parse(tempString, usCulture); tempBook.Price = d; listOfBooks.Add(tempBook); } break; case Bookenum.Description: if (row.Field<String>("description").ToUpper().Contains(searchQuery)) { tempBook.Title = row.Field<String>("title"); tempBook.Author = row.Field<String>("author"); tempBook.Description = row.Field<String>("description"); tempBook.Genre = row.Field<String>("genre"); tempBook.Publish_date = row.Field<String>("publish_date"); tempBook.Id = row.Field<String>("id"); //Changing to us culture so the price can be parsed correctly to decimal. System.Globalization.CultureInfo usCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); String tempString = row.Field<String>("price"); decimal d = Decimal.Parse(tempString, usCulture); tempBook.Price = d; listOfBooks.Add(tempBook); } break; case Bookenum.Genre: if (row.Field<String>("genre").ToUpper().Contains(searchQuery)) { tempBook.Title = row.Field<String>("title"); tempBook.Author = row.Field<String>("author"); tempBook.Description = row.Field<String>("description"); tempBook.Genre = row.Field<String>("genre"); tempBook.Publish_date = row.Field<String>("publish_date"); tempBook.Id = row.Field<String>("id"); //Changing to us culture so the price can be parsed correctly to decimal. System.Globalization.CultureInfo usCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); String tempString = row.Field<String>("price"); decimal d = Decimal.Parse(tempString, usCulture); tempBook.Price = d; listOfBooks.Add(tempBook); } break; case Bookenum.Id: if (row.Field<String>("id").Contains(searchQuery)) { tempBook.Title = row.Field<String>("title"); tempBook.Author = row.Field<String>("author"); tempBook.Description = row.Field<String>("description"); tempBook.Genre = row.Field<String>("genre"); tempBook.Publish_date = row.Field<String>("publish_date"); tempBook.Id = row.Field<String>("id"); //Changing to us culture so the price can be parsed correctly to decimal. System.Globalization.CultureInfo usCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); String tempString = row.Field<String>("price"); decimal d = Decimal.Parse(tempString, usCulture); tempBook.Price = d; listOfBooks.Add(tempBook); } break; case Bookenum.Price: if (row.Field<String>("price").Contains(searchQuery)) { tempBook.Title = row.Field<String>("title"); tempBook.Author = row.Field<String>("author"); tempBook.Description = row.Field<String>("description"); tempBook.Genre = row.Field<String>("genre"); tempBook.Publish_date = row.Field<String>("publish_date"); tempBook.Id = row.Field<String>("id"); //Changing to us culture so the price can be parsed correctly to decimal. System.Globalization.CultureInfo usCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); String tempString = row.Field<String>("price"); decimal d = Decimal.Parse(tempString, usCulture); tempBook.Price = d; listOfBooks.Add(tempBook); } break; case Bookenum.Publishdate: if (row.Field<String>("publish_date").Contains(searchQuery)) { tempBook.Title = row.Field<String>("title"); tempBook.Author = row.Field<String>("author"); tempBook.Description = row.Field<String>("description"); tempBook.Genre = row.Field<String>("genre"); tempBook.Publish_date = row.Field<String>("publish_date"); tempBook.Id = row.Field<String>("id"); //Changing to us culture so the price can be parsed correctly to decimal. System.Globalization.CultureInfo usCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); String tempString = row.Field<String>("price"); decimal d = Decimal.Parse(tempString, usCulture); tempBook.Price = d; listOfBooks.Add(tempBook); } break; case Bookenum.Title: if (row.Field<String>("title").ToUpper().Contains(searchQuery)) { tempBook.Title = row.Field<String>("title"); tempBook.Author = row.Field<String>("author"); tempBook.Description = row.Field<String>("description"); tempBook.Genre = row.Field<String>("genre"); tempBook.Publish_date = row.Field<String>("publish_date"); tempBook.Id = row.Field<String>("id"); //Changing to us culture so the price can be parsed correctly to decimal. System.Globalization.CultureInfo usCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); String tempString = row.Field<String>("price"); decimal d = Decimal.Parse(tempString, usCulture); tempBook.Price = d; listOfBooks.Add(tempBook); } break; } } } return listOfBooks; }