static void Main(string[] args) { List <Product> products = new List <Product>(); // read northwind using (var db = new Northwind()) { products = db.Products.OrderBy(p => p.ProductName).Take(3).ToList(); } products.ForEach(p => Console.WriteLine(p.ProductName)); // extract Products Console.WriteLine("\n\nExtracting To XML\n\n"); var xml = new XElement("Products", from p in products select new XElement("Product", new XElement("ProductID", p.ProductID), new XElement("Cost", p.Cost), new XElement("ProductName", p.ProductName) )); // Write to XML Console.WriteLine(xml.ToString()); // Write to File var doc = new XDocument(xml); doc.Save("Products.xml"); // now the test Console.WriteLine("\n\nFirstly just read back the raw XML data as a string\n\n"); Console.WriteLine(File.ReadAllText("Products.xml")); // as XML document var doc2 = XDocument.Load("Products.xml"); // Recap on what achieved using (var db = new Northwind()) { products = db.Products.Take(5).ToList(); } // Created XML document from this list of products var xml5 = new XElement("Products", from p in products select new XElement("Product", new XElement("ProductID", p.ProductID), new XElement("ProductName", p.ProductName), new XElement("Cost", p.Cost))); // Write to disk var xmlDocument5 = new XDocument(xml5); xmlDocument5.Save("FiveProducts.xml"); // Read back to string Console.WriteLine("\n\nRead back 5 products\n\n"); Console.WriteLine(File.ReadAllText("FiveProducts.xml")); // deserialize Console.WriteLine("\n\nDeserialize back into PRODUCT OBJECTS\n\n"); // CREATE STRUCTURE TO HOLD LIST OF DESERIALIZED OBJECTS var productList = new Products(); // Use streaming to get data here using (var reader = new StreamReader("FiveProducts.xml")) { // DESERIALIZE BACK INTO PRODUCTS var serializer = new XmlSerializer(typeof(Products)); // do the work productList = (Products)serializer.Deserialize(reader); } // job done; just output the list and have look foreach (Product p in productList.ProductList) { Console.WriteLine($"{p.ProductID,-10}{p.ProductName,-50}{p.Cost}"); } }
static void Main(string[] args) { List <Product> products = new List <Product>(); //Add Product/ Category / Northwind classes from last //CORE project //Read Northwind using (var db = new Northwind()) { products = db.Products.OrderBy(p => p.ProductName).Take(3).ToList(); } products.ForEach(p => Console.WriteLine(p.ProductName)); //Extract Products Console.WriteLine("\n\nExtracting To XML \n\n"); var xml = new XElement("Products", from p in products select new XElement("product", new XAttribute("id", p.ProductID), new XAttribute("price", p.Cost), new XAttribute("name", p.ProductName) )); //Write to XML Console.WriteLine(xml.ToString()); //Write to File var doc = new XDocument(xml); doc.Save("Products.xml"); //now the test Console.WriteLine("\nFirstly just read back the raw XML data as a string\n\n"); Console.WriteLine(File.ReadAllText("Products.xml")); //as XML document var doc2 = XDocument.Load("Products.xml"); //Recap on what achieved using (var db = new Northwind()) { products = db.Products.Take(5).ToList(); } //Created XML document from this list of products var xml5 = new XElement("Products", from p in products select new XElement("Product", new XElement("ProductID", p.ProductID), new XElement("ProductName", p.ProductName), new XElement("Cost", p.Cost))); //Write to disk var xmlDocument5 = new XDocument(xml5); xmlDocument5.Save("FiveProducts.xml"); //Read back to string Console.WriteLine("\n\nRead back 5 products \n\n"); Console.WriteLine(File.ReadAllText("FiveProducts.xml")); //Deserialize Console.WriteLine("\n\n Deserialize back into PRODUCT OBJECTS\n\n"); //Create structure to hold list of deserialized objects var productList = new Products(); //use streaming to get data here using (var reader = new StreamReader("FiveProducts.xml")) { //deserialize back into products var serializer = new XmlSerializer(typeof(Products)); //Do the work productList = (Products)serializer.Deserialize(reader); } //job done - just output the list and have a look foreach (Product p in productList.ProductList) { Console.WriteLine($"{p.ProductID}{p.ProductName}{p.Cost}"); } }