usingSystem;usingSystem.Xml;usingSystem.Xml.Serialization;usingSystem.IO;/* The XmlRootAttribute allows you to set an alternate name (PurchaseOrder) of the XML element, the element namespace; by default, the XmlSerializer uses the class name. The attribute also allows you to set the XML namespace for the element. Lastly, the attribute sets the IsNullable property, which specifies whether the xsi:null attribute appears if the class instance is set to a null reference. */[XmlRootAttribute("PurchaseOrder", Namespace="http://www.cpandl.com",IsNullable = false)]publicclassPurchaseOrder{publicAddressShipTo;publicstringOrderDate;/* The XmlArrayAttribute changes the XML element name from the default of "OrderedItems" to "Items". */[XmlArrayAttribute("Items")]publicOrderedItem[]OrderedItems;publicdecimalSubTotal;publicdecimalShipCost;publicdecimalTotalCost;}publicclassAddress{/* The XmlAttribute instructs the XmlSerializer to serialize the Name field as an XML attribute instead of an XML element (the default behavior). */[XmlAttribute]publicstringName;publicstringLine1;/* Setting the IsNullable property to false instructs the XmlSerializer that the XML attribute will not appear if the City field is set to a null reference. */[XmlElementAttribute(IsNullable = false)]publicstringCity;publicstringState;publicstringZip;}publicclassOrderedItem{publicstringItemName;publicstringDescription;publicdecimalUnitPrice;publicintQuantity;publicdecimalLineTotal;/* Calculate is a custom method that calculates the price per item, and stores the value in a field. */publicvoidCalculate(){LineTotal=UnitPrice*Quantity;}}publicclassTest{publicstaticvoidMain(){// Read and write purchase orders.Testt=newTest();t.CreatePO("po.xml");t.ReadPO("po.xml");}privatevoidCreatePO(stringfilename){// Create an instance of the XmlSerializer class;// specify the type of object to serialize.XmlSerializerserializer=newXmlSerializer(typeof(PurchaseOrder));TextWriterwriter=newStreamWriter(filename);PurchaseOrderpo=newPurchaseOrder();// Create an address to ship and bill to.AddressbillAddress=newAddress();billAddress.Name="Teresa Atkinson";billAddress.Line1="1 Main St.";billAddress.City="AnyTown";billAddress.State="WA";billAddress.Zip="00000";// Set ShipTo and BillTo to the same addressee.po.ShipTo=billAddress;po.OrderDate=System.DateTime.Now.ToLongDateString();// Create an OrderedItem object.OrderedItemi1=newOrderedItem();i1.ItemName="Widget S";i1.Description="Small widget";i1.UnitPrice=(decimal)5.23;i1.Quantity=3;i1.Calculate();// Insert the item into the array.OrderedItem[]items={i1};po.OrderedItems=items;// Calculate the total cost.decimalsubTotal=newdecimal();foreach(OrderedItemoiinitems){subTotal+=oi.LineTotal;}po.SubTotal=subTotal;po.ShipCost=(decimal)12.51;po.TotalCost=po.SubTotal+po.ShipCost;// Serialize the purchase order, and close the TextWriter.serializer.Serialize(writer,po);writer.Close();}protectedvoidReadPO(stringfilename){// Create an instance of the XmlSerializer class;// specify the type of object to be deserialized.XmlSerializerserializer=newXmlSerializer(typeof(PurchaseOrder));/* If the XML document has been altered with unknown nodes or attributes, handle them with the UnknownNode and UnknownAttribute events.*/serializer.UnknownNode+=newXmlNodeEventHandler(serializer_UnknownNode);serializer.UnknownAttribute+=newXmlAttributeEventHandler(serializer_UnknownAttribute);// A FileStream is needed to read the XML document.FileStreamfs=newFileStream(filename,FileMode.Open);// Declare an object variable of the type to be deserialized.PurchaseOrderpo;/* Use the Deserialize method to restore the object's state with data from the XML document. */po=(PurchaseOrder)serializer.Deserialize(fs);// Read the order date.Console.WriteLine("OrderDate: "+po.OrderDate);// Read the shipping address.AddressshipTo=po.ShipTo;ReadAddress(shipTo,"Ship To:");// Read the list of ordered items.OrderedItem[]items=po.OrderedItems;Console.WriteLine("Items to be shipped:");foreach(OrderedItemoiinitems){Console.WriteLine("\t"+oi.ItemName+"\t"+oi.Description+"\t"+oi.UnitPrice+"\t"+oi.Quantity+"\t"+oi.LineTotal);}// Read the subtotal, shipping cost, and total cost.Console.WriteLine("\t\t\t\t\t Subtotal\t"+po.SubTotal);Console.WriteLine("\t\t\t\t\t Shipping\t"+po.ShipCost);Console.WriteLine("\t\t\t\t\t Total\t\t"+po.TotalCost);}protectedvoidReadAddress(Addressa,stringlabel){// Read the fields of the Address object.Console.WriteLine(label);Console.WriteLine("\t"+a.Name);Console.WriteLine("\t"+a.Line1);Console.WriteLine("\t"+a.City);Console.WriteLine("\t"+a.State);Console.WriteLine("\t"+a.Zip);Console.WriteLine();}privatevoidserializer_UnknownNode(objectsender,XmlNodeEventArgse){Console.WriteLine("Unknown Node:"+e.Name+"\t"+e.Text);}privatevoidserializer_UnknownAttribute(objectsender,XmlAttributeEventArgse){System.Xml.XmlAttributeattr=e.Attr;Console.WriteLine("Unknown attribute "+attr.Name+"='"+attr.Value+"'");}}