예제 #1
0
        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");
        }
예제 #2
0
 private void button1_Click(object sender, EventArgs e)
 {
     //new products object
     Product pd = new Product();
     //set some properties
     pd.ProductID = 200;
     pd.CategoryID = 100;
     pd.Discontinued = false;
     pd.ProductName = "Serialize Objects";
     pd.QuantityPerUnit = "6";
     pd.ReorderLevel = 1;
     pd.SupplierID = 1;
     pd.UnitPrice = 1000;
     pd.UnitsInStock = 10;
     pd.UnitsOnOrder = 0;
     //new TextWriter and XmlSerializer
     TextWriter tr = new StreamWriter("serialprod.xml");
     XmlSerializer sr = new XmlSerializer(typeof(Product));
     //serialize object
     sr.Serialize(tr, pd);
     tr.Close();
     webBrowser1.Navigate(AppDomain.CurrentDomain.BaseDirectory + "serialprod.xml");
 }
예제 #3
0
        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");

        }