static void Main(string[] args) { //string xmlString = "<products><ID>1</ID><Name>Chai</Name><UnitPrice>18.0000</UnitPrice><Quantity>10 boxes x 20 bags</Quantity><ReorderLevel>10</ReorderLevel><CategoryID>1</CategoryID></products>"; //XmlSerializer serializer = new XmlSerializer(typeof(List<Product>), new XmlRootAttribute("Products")); //StringReader stringReader = new StringReader(xmlString); //ProductList = (List<Product>)serializer.Deserialize(stringReader); using (var db = new Northwind()) { ProductList = db.Products.ToList(); // ProductList.ForEach(o => Console.WriteLine(o.ProductName)); product = ProductList.FirstOrDefault(); product2 = ProductList.Skip(1).FirstOrDefault(); var xmlProduct = new XElement("products", new XElement("ID", product.ProductID), new XElement("Name", product.ProductName), new XElement("UnitPrice", product.UnitPrice), new XElement("Quantity", product.QuantityPerUnit), new XElement("ReorderLevel", product.ReorderLevel), new XElement("CategoryID", product.CategoryID) ); Console.WriteLine(xmlProduct); // xmlProduct.Save("ProductsList.xml"); // loop var xmlProduct2 = new XElement("products", from pl in ProductList select new XElement("products", new XElement("ID", pl.ProductID), new XElement("Name", pl.ProductName), new XElement("UnitPrice", pl.UnitPrice), new XElement("Quantity", pl.QuantityPerUnit), new XElement("ReorderLevel", pl.ReorderLevel), new XElement("CategoryID", pl.CategoryID) )); Console.WriteLine(xmlProduct2); //write new file to cml then de-serialize it back //write new file productSimpleList = db.Products.ToList(); var xmlProductsOutput = new XElement( "Products", from p in productSimpleList select new XElement("Product", new XElement("ProductID", p.ProductID), new XElement("ProductName", p.ProductName), new XElement("CategoryID", p.CategoryID) )); // xmlProductsOutput.Save("xmlProductsOutput3.xml"); // lets deserialise now! Console.WriteLine("\n\nPrinting out list of Deserialized Products\n\n"); var productsDeserialized = new Products(); using (var reader = new StreamReader("xmlProductsOutput3.xml")) //reading the file { var serialiser = new XmlSerializer(typeof(Products)); productsDeserialized = (Products)serialiser.Deserialize(reader); } //at this point products Deserialized shuld hold our Product List productsDeserialized.ProductList.ForEach(p => { Console.WriteLine($"{p.ProductID,-10} {p.ProductName,-45} {p.CategoryID}"); }); } }
static void Main(string[] args) { using (var db = new Northwind()) { products = db.Products.ToList(); products.ForEach(p => Console.WriteLine(p.ProductName)); // just get first product product = products.FirstOrDefault(); product2 = products.Skip(1).FirstOrDefault(); Console.WriteLine("\n\nFirst Product\n"); var xmlProduct = new XElement("Product", new XElement("ProductID", product.ProductID), new XElement("ProductName", product.ProductName), new XElement("UnitPrice", product.Cost), new XElement("CategoryID", product.CategoryID) ); Console.WriteLine(xmlProduct); // save to file xmlProduct.Save("xmlOneProduct.xml"); var xmlProductSave = new XDocument(XElement.Parse(xmlProduct.ToString())); var xmlProductSave2 = new XDocument(xmlProduct); xmlProductSave.Save("xmlOneProductv2.xml"); Console.WriteLine("\n\nNow listing all products"); Console.WriteLine("============================\n"); var xmlProducts = new XElement("Products", from p in products select new XElement("Product", new XElement("ProductID", p.ProductID), new XElement("ProductName", p.ProductName), new XElement("UnitPrice", p.Cost), new XElement("CategoryID", p.CategoryID)) ); Console.WriteLine(xmlProducts); //Console.WriteLine("\n\nNow listing all products"); //Console.WriteLine("============================\n"); //var xmlProductsWithCategory = new XElement("Products", // from p in products // select new XElement("Product", // new XElement("ProductID", p.ProductID), // new XElement("ProductName", p.ProductName), // new XElement("UnitPrice", p.Cost), // new XElement("Category",p.Category.CategoryName) // ) // ); //Console.WriteLine(xmlProductsWithCategory); //xmlProductsWithCategory.Save("Products.xml"); // write new file to xml then de-serialize it back // write new file products = db.Products.ToList(); var xmlProductsOutput = new XElement( "Products", from product in products select new XElement( "Product", new XElement("ProductID", product.ProductID), new XElement("ProductName", product.ProductName), new XElement("CategoryID", product.CategoryID) ) ); xmlProductsOutput.Save("xmlProductsOutput.xml"); Console.WriteLine("\n\nNow Print Out List Of Deserialized Products\n"); var productsDeserialized = new Products(); using (var reader = new StreamReader("xmlProductsOutput.xml")) { XmlSerializer serializer = new XmlSerializer(typeof(Products)); productsDeserialized = (Products)serializer.Deserialize(reader); } // at this point productsDeserialized should hold our Product List productsDeserialized.ProductList.ForEach(p => { Console.WriteLine($"{p.ProductID,-10}{p.ProductName,-25},{p.CategoryID}"); }); } }