static RootLibraryElement Read(string filePath) { var root = new RootLibraryElement(); var worker = new XMLWorker(); foreach (var el in worker.ReadXMLFromFile(filePath)) { root.Elements.Add(TryParse(el)); } return(root); }
static void Write() { var root = new RootLibraryElement(); var book = new BookLibraryElement(); book.Name = "TestBook"; book.PageCount = 34; book.PublicationPlace = "Karaganda"; book.PublisherName = "Fgfgf"; book.PublishYear = 1943; book.ISBN = "5786v45677v76487"; book.Comment = "fgkllrlljjklghhgfdhfhgfhgf"; book.Authors = "Villain"; root.Elements.Add(book); var paper = new NewspaperLibraryElement(); paper.Name = "TestPaper"; paper.PageCount = 25; paper.PublicationPlace = "Karaganda"; paper.PublisherName = "Namenaem"; paper.PublishYear = 2001; paper.ISSN = "456754547687"; paper.Comment = "fgkоднакоhgf"; paper.Date = DateTime.Now; paper.Number = 856; root.Elements.Add(paper); var patent = new PatentLibraryElement(); patent.Name = "TestPatent"; patent.PageCount = 125; patent.ApplicationDate = DateTime.Now.Date.AddDays(-2); patent.PublicationDate = DateTime.Now.Date.AddDays(-1); patent.Creator = "Леша изобрел что-то там"; patent.Country = "Гваделупа"; patent.Comment = "вот какая она"; patent.RegistrationNumber = 766456; root.Elements.Add(patent); var worker = new XMLWorker(); worker.WriteToXMLFile(root, @"D:\XMLFileWriteTest.xml"); Console.ReadKey(); }
public void ReadXMLFromFileWorksCorrectly() { var worker = new XMLWorker(); var root2 = new RootLibraryElement(); foreach (var el in worker.ReadXMLFromFile(@"C:\XMLFileWrite.xml")) { root2.Elements.Add(TryParse(el)); } Assert.IsTrue(root2.Elements.Count == 3); Assert.IsInstanceOfType(root2.Elements.First(), typeof(BookLibraryElement)); Assert.IsInstanceOfType(root2.Elements.Skip(1).First(), typeof(NewspaperLibraryElement)); Assert.IsInstanceOfType(root2.Elements.Skip(2).First(), typeof(PatentLibraryElement)); Assert.AreEqual("TestBook", (root2.Elements.First() as BookLibraryElement).Name); Assert.AreEqual("TestPaper", (root2.Elements.Skip(1).First() as NewspaperLibraryElement).Name); Assert.AreEqual("TestPatent", (root2.Elements.Skip(2).First() as PatentLibraryElement).Name); }
static void Write(RootLibraryElement from, string toFile) { var worker = new XMLWorker(); worker.WriteToXMLFile(from, toFile); }
public void WriteToXMLFile(RootLibraryElement library, string filePath) { var lib = new XElement("root"); foreach (var el in library.Elements) { if (el is BookLibraryElement) { var book = el as BookLibraryElement; if (!BookValidate(book)) { break; } var bookElement = new XElement("libraryElement", new XAttribute("type", "book")); bookElement.Add(new XElement("name", book.Name)); bookElement.Add(new XElement("authors", book.Authors)); bookElement.Add(new XElement("city", book.PublicationPlace)); bookElement.Add(new XElement("publisher", book.PublisherName)); bookElement.Add(new XElement("year", book.PublishYear)); bookElement.Add(new XElement("pageCount", book.PageCount)); bookElement.Add(new XElement("comment", book.Comment)); bookElement.Add(new XElement("ISBN", book.ISBN)); lib.Add(bookElement); } else if (el is NewspaperLibraryElement) { var paper = el as NewspaperLibraryElement; if (!NewspaperValidate(paper)) { break; } var paperElement = new XElement("libraryElement", new XAttribute("type", "newspaper")); paperElement.Add(new XElement("name", paper.Name)); paperElement.Add(new XElement("city", paper.PublicationPlace)); paperElement.Add(new XElement("publisher", paper.PublisherName)); paperElement.Add(new XElement("year", paper.PublishYear)); paperElement.Add(new XElement("pageCount", paper.PageCount)); paperElement.Add(new XElement("comment", paper.Comment)); paperElement.Add(new XElement("number", paper.Number)); paperElement.Add(new XElement("date", paper.Date.ToString("yyyy-mm-dd", new CultureInfo("us-US").DateTimeFormat))); paperElement.Add(new XElement("ISSN", paper.ISSN)); lib.Add(paperElement); } else if (el is PatentLibraryElement) { var patent = el as PatentLibraryElement; if (!PatentValidate(patent)) { break; } var patentElement = new XElement("libraryElement", new XAttribute("type", "patent")); patentElement.Add(new XElement("name", patent.Name)); patentElement.Add(new XElement("creator", patent.Creator)); patentElement.Add(new XElement("country", patent.Country)); patentElement.Add(new XElement("registrationNumber", patent.RegistrationNumber)); patentElement.Add(new XElement("applicationDate", patent.ApplicationDate.ToString("yyyy-mm-dd", new CultureInfo("us-US").DateTimeFormat))); patentElement.Add(new XElement("publicationDate", patent.PublicationDate.ToString("yyyy-mm-dd", new CultureInfo("us-US").DateTimeFormat))); patentElement.Add(new XElement("pageCount", patent.PageCount)); patentElement.Add(new XElement("comment", patent.Comment)); lib.Add(patentElement); } } lib.Save(filePath); }