static void CreateBookStudentRandom(int countOfBS) { XElement rootStudent = new Student().ReadFromXml("Students.xml"); XElement rootBook = new Book().ReadFromXml("Books.xml"); List <Book> books = Book.ConvertXelementToListBooks(rootBook); List <Student> students = Student.ConvertXelementToListStudent(rootStudent); List <BookAndStudent> BS = new List <BookAndStudent>(); Random rmd = new Random(); for (int i = 0; i < countOfBS; i++) { Book tmpBook = books[rmd.Next(0, 10)]; Student tmpStd = students[rmd.Next(0, 10)]; BookAndStudent tmp = BookAndStudent.CreateNote(tmpBook, tmpStd); BS.Add(tmp); } for (int i = 0; i < BS.Count; i++) { BS[i].WriteTOXml("BooksAndStudents.xml"); } }
/// <summary> /// Convert to BookStudent /// </summary> /// <param name="xel"></param> /// <returns></returns> public static List <BookAndStudent> ConvertXelementToListBookStudent(XElement xel) { List <BookAndStudent> listBook = new List <BookAndStudent>(); foreach (XElement item in xel.Elements("bookandstudent")) { BookAndStudent tmp = new BookAndStudent { Book = new Book(), Student = new Student() }; tmp.Student.Address = item.Element("address").Value; tmp.Student.Name = item.Element("name").Value; tmp.Student.Faculty = item.Element("faculty").Value; tmp.Student.Group = item.Element("faculty").Attribute("group").Value; tmp.Student.Course = item.Element("faculty").Attribute("course").Value; tmp.Book.Title = item.Element("title").Value; tmp.Book.Author = item.Element("author").Value; tmp.Book.Edition = item.Element("edition").Value; tmp.Book.Pages = Convert.ToInt32(item.Element("edition").Attribute("pages").Value); tmp.Book.Price = Convert.ToInt32(item.Element("edition").Attribute("price").Value); tmp.StartRead = item.Element("getdate").Value; tmp.EndRead = item.Element("returndate").Value; listBook.Add(tmp); } return(listBook); }
static void Main(string[] args) { //CreateRandomBook(10); //CreateRandomStudent(10); //CreateBookStudentRandom(50); XElement el = new BookAndStudent().ReadFromXml("BooksAndStudents.xml"); List <BookAndStudent> tmp = BookAndStudent.ConvertXelementToListBookStudent(el); BookAndStudent del = tmp.FirstOrDefault(x => x.Student.Name == "Studnet"); tmp.Remove(del); BookAndStudent.WriteListBookStudent(tmp, "BooksAndStudents.xml"); Console.Read(); }
/// <summary> /// Create BookAndStudent /// </summary> /// <param name="book"></param> /// <param name="student"></param> /// <returns></returns> public static BookAndStudent CreateNote(Book book, Student student) { if (book == null || student == null) { return(null); } BookAndStudent tmp = new BookAndStudent(); tmp.Book = book; tmp.Student = student; tmp.StartRead = DateTime.Now.ToString(); tmp.EndRead = DateTime.Now.ToString(); return(tmp); }