private void button3_Click(object sender, EventArgs e) { //create new book and bookproducts objects Product newProd = new Product(); BookProduct newBook = new BookProduct(); //set som eproperties newProd.ProductID = 100; newProd.ProductName = "Product Thing"; newProd.SupplierID = 10; newBook.ProductID = 101; newBook.ProductName = "How to Use Your New Product Thing"; newBook.SupplierID = 10; newBook.ISBN = "123456789"; //add the items to an array Product[] addProd = { newProd, newBook }; //new inventory object using the addProd array Inventory inv = new Inventory(); inv.InventoryItems = addProd; //serialize the Inventory object TextWriter tr = new StreamWriter("order.xml"); XmlSerializer sr = new XmlSerializer(typeof(Inventory)); sr.Serialize(tr, inv); tr.Close(); webBrowser1.Navigate(AppDomain.CurrentDomain.BaseDirectory + "order.xml"); }
private void button4_Click(object sender, EventArgs e) { //create the XmlAttributes object XmlAttributes attrs = new XmlAttributes(); //add the types of the objects that will be serialized attrs.XmlElements.Add(new XmlElementAttribute("Book", typeof(BookProduct))); attrs.XmlElements.Add(new XmlElementAttribute("Product", typeof(Product))); XmlAttributeOverrides attrOver = new XmlAttributeOverrides(); //add to the attributes collection attrOver.Add(typeof(Inventory), "InventoryItems", attrs); //create the Product and Book objects Product newProd = new Product(); BookProduct newBook = new BookProduct(); newProd.ProductID = 100; newProd.ProductName = "Product Thing"; newProd.SupplierID = 10; newBook.ProductID = 101; newBook.ProductName = "How to Use Your New Product Thing"; newBook.SupplierID = 10; newBook.ISBN = "123456789"; Product[] addProd = { newProd, newBook }; Inventory inv = new Inventory(); inv.InventoryItems = addProd; TextWriter tr = new StreamWriter("inventory.xml"); XmlSerializer sr = new XmlSerializer(typeof(Inventory), attrOver); sr.Serialize(tr, inv); tr.Close(); webBrowser1.Navigate(AppDomain.CurrentDomain.BaseDirectory + "inventory.xml"); }