potest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Xml;
  3. using System.Xml.Serialization;
  4. using System.IO;
  5. // The XmlRootAttribute allows you to set an alterate name
  6. // (PurchaseOrder) for the XML element and its namespace. By
  7. // default, the XmlSerializer uses the class name. The attribute
  8. // also allows you to set the XML namespace for the element. Lastly,
  9. // the attribute sets the IsNullable property, which specifies whether
  10. // the xsi:null attribute appears if the class instance is set to
  11. // a null reference.
  12. [XmlRootAttribute("PurchaseOrder", Namespace="http://cpandl.com",
  13. IsNullable=false)]
  14. public class PurchaseOrder
  15. {
  16. public Address ShipTo;
  17. public string OrderDate;
  18. // The XmlArrayAttribute changes the XML element name
  19. // from the default of "OrderedItems" to "Items".
  20. [XmlArrayAttribute("Items")]
  21. public OrderedItem[] OrderedItems;
  22. public decimal SubTotal;
  23. public decimal ShipCost;
  24. public decimal TotalCost;
  25. }
  26. public class Address
  27. {
  28. // The XmlAttribute instructs the XmlSerializer to serialize the Name
  29. // field as an XML attribute instead of an XML element (the default
  30. // behaviour).
  31. [XmlAttribute]
  32. public string Name;
  33. public string Line1;
  34. // Setting the IsNullable property to false instructs the
  35. // XmlSerializer that the XML attribute will not appear if
  36. // the City field is set to a null reference.
  37. [XmlElementAttribute(IsNullable=false)]
  38. public string City;
  39. public string State;
  40. public string Zip;
  41. }
  42. public class OrderedItem
  43. {
  44. public string ItemName;
  45. public string Description;
  46. public decimal UnitPrice;
  47. public int Quantity;
  48. public decimal LineTotal;
  49. // Calculate is a custom method that calculates the price per item
  50. // and stores the value in a field.
  51. public void Calculate()
  52. {
  53. LineTotal=UnitPrice*Quantity;
  54. }
  55. }
  56. public class Test
  57. {
  58. public static void Main()
  59. {
  60. // Read and write purchase orders.
  61. Test t=new Test();
  62. t.CreatePO("potest.xml");
  63. t.ReadPO("potest.xml");
  64. }
  65. private void CreatePO(string filename)
  66. {
  67. // Creates an instance of the XmlSerializer class;
  68. // specifies the type of object to serialize.
  69. XmlSerializer serializer=new XmlSerializer(typeof(PurchaseOrder));
  70. TextWriter writer=new StreamWriter(filename);
  71. PurchaseOrder po=new PurchaseOrder();
  72. // Creates an address to ship and bill to.
  73. Address billAddress=new Address();
  74. billAddress.Name="Teresa Atkinson";
  75. billAddress.Line1="1 Main St.";
  76. billAddress.City="AnyTown";
  77. billAddress.State="WA";
  78. billAddress.Zip="00000";
  79. // Sets ShipTo and BillTo to the same addressee.
  80. po.ShipTo=billAddress;
  81. po.OrderDate=System.DateTime.Now.ToLongDateString();
  82. // Creates an OrderedItem.
  83. OrderedItem i1=new OrderedItem();
  84. i1.ItemName="Widget S";
  85. i1.Description="Small widget";
  86. i1.UnitPrice=(decimal)5.23;
  87. i1.Quantity=3;
  88. i1.Calculate();
  89. // Inserts the item into the array.
  90. OrderedItem[] items={i1};
  91. po.OrderedItems=items;
  92. // Calculate the total cost.
  93. decimal subTotal=new decimal();
  94. foreach(OrderedItem oi in items)
  95. {
  96. subTotal+=oi.LineTotal;
  97. }
  98. po.SubTotal=subTotal;
  99. po.ShipCost=(decimal)12.51;
  100. po.TotalCost=po.SubTotal+po.ShipCost;
  101. // Serializes the purchase order, and closes the TextWriter.
  102. serializer.Serialize(writer, po);
  103. writer.Close();
  104. }
  105. protected void ReadPO(string filename)
  106. {
  107. // Creates an instance of the XmlSerializer class;
  108. // specifies the type of object to be deserialized.
  109. XmlSerializer serializer=new XmlSerializer(typeof(PurchaseOrder));
  110. // If the XML document has been altered with unknown
  111. // nodes or attributes, handles them with the
  112. // UnknownNode and UnknownAttribute events.
  113. serializer.UnknownNode+=new XmlNodeEventHandler(serializer_UnknownNode);
  114. serializer.UnknownAttribute+=new XmlAttributeEventHandler(serializer_UnknownAttribute);
  115. // A FileStream is needed to read the XML document.
  116. FileStream fs=new FileStream(filename, FileMode.Open);
  117. // Declares an object variable of the type to be deserialized.
  118. PurchaseOrder po;
  119. // Uses the Deserialize method to restore the object's state with
  120. // data from the XML document. */
  121. po=(PurchaseOrder)serializer.Deserialize(fs);
  122. fs.Close();
  123. // Reads the order date.
  124. Console.WriteLine("OrderDate: "+po.OrderDate);
  125. // Reads the shipping address.
  126. Address shipTo=po.ShipTo;
  127. ReadAddress(shipTo, "Ship To:");
  128. // Reads the list of ordered items.
  129. OrderedItem[] items=po.OrderedItems;
  130. Console.WriteLine("Items to be shipped:");
  131. foreach(OrderedItem oi in items)
  132. {
  133. Console.WriteLine("\t"+
  134. oi.ItemName+"\t"+
  135. oi.Description+"\t"+
  136. oi.UnitPrice+"\t"+
  137. oi.Quantity+"\t"+
  138. oi.LineTotal);
  139. }
  140. // Reads the subtotal, shipping cost, and total cost.
  141. Console.WriteLine("\n\t\t\t\t\t Subtotal\t"+po.SubTotal+
  142. "\n\t\t\t\t\t Shipping\t"+po.ShipCost+
  143. "\n\t\t\t\t\t Total\t\t"+po.TotalCost);
  144. }
  145. protected void ReadAddress(Address a, string label)
  146. {
  147. // Reads the fields of the Address.
  148. Console.WriteLine(label);
  149. Console.Write("\t"+
  150. a.Name+"\n\t"+
  151. a.Line1+"\n\t"+
  152. a.City+"\t"+
  153. a.State+"\n\t"+
  154. a.Zip+"\n");
  155. }
  156. protected void serializer_UnknownNode(object sender,
  157. XmlNodeEventArgs e)
  158. {
  159. Console.WriteLine("Unknown Node:"+e.Name+"\t"+e.Text);
  160. }
  161. protected void serializer_UnknownAttribute(object sender,
  162. XmlAttributeEventArgs e)
  163. {
  164. System.Xml.XmlAttribute attr=e.Attr;
  165. Console.WriteLine("Unknown attribute "+attr.Name+"='"+attr.Value+"'");
  166. }
  167. }