code_junkie

How to Deserialize XML document

Nov 12th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Cars>
  3. <Car>
  4. <StockNumber>1020</StockNumber>
  5. <Make>Nissan</Make>
  6. <Model>Sentra</Model>
  7. </Car>
  8. <Car>
  9. <StockNumber>1010</StockNumber>
  10. <Make>Toyota</Make>
  11. <Model>Corolla</Model>
  12. </Car>
  13. <Car>
  14. <StockNumber>1111</StockNumber>
  15. <Make>Honda</Make>
  16. <Model>Accord</Model>
  17. </Car>
  18. </Cars>
  19.  
  20. [Serializable()]
  21. public class Car
  22. {
  23. [System.Xml.Serialization.XmlElementAttribute("StockNumber")]
  24. public string StockNumber{ get; set; }
  25.  
  26. [System.Xml.Serialization.XmlElementAttribute("Make")]
  27. public string Make{ get; set; }
  28.  
  29. [System.Xml.Serialization.XmlElementAttribute("Model")]
  30. public string Model{ get; set; }
  31. }
  32.  
  33. [System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
  34. public class Cars
  35. {
  36. [XmlArrayItem(typeof(Car))]
  37. public Car[] Car { get; set; }
  38.  
  39. }
  40.  
  41. public class CarSerializer
  42. {
  43. public Cars Deserialize()
  44. {
  45. Cars[] cars = null;
  46. string path = HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data/") + "cars.xml";
  47.  
  48. XmlSerializer serializer = new XmlSerializer(typeof(Cars[]));
  49.  
  50. StreamReader reader = new StreamReader(path);
  51. reader.ReadToEnd();
  52. cars = (Cars[])serializer.Deserialize(reader);
  53. reader.Close();
  54.  
  55. return cars;
  56. }
  57. }
  58.  
  59. [Serializable()]
  60. public class Car
  61. {
  62. [System.Xml.Serialization.XmlElement("StockNumber")]
  63. public string StockNumber { get; set; }
  64.  
  65. [System.Xml.Serialization.XmlElement("Make")]
  66. public string Make { get; set; }
  67.  
  68. [System.Xml.Serialization.XmlElement("Model")]
  69. public string Model { get; set; }
  70. }
  71.  
  72.  
  73. [Serializable()]
  74. [System.Xml.Serialization.XmlRoot("CarCollection")]
  75. public class CarCollection
  76. {
  77. [XmlArray("Cars")]
  78. [XmlArrayItem("Car", typeof(Car))]
  79. public Car[] Car { get; set; }
  80. }
  81.  
  82. CarCollection cars = null;
  83. string path = "cars.xml";
  84.  
  85. XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));
  86.  
  87. StreamReader reader = new StreamReader(path);
  88. cars = (CarCollection)serializer.Deserialize(reader);
  89. reader.Close();
  90.  
  91. <?xml version="1.0" encoding="utf-8"?>
  92. <CarCollection>
  93. <Cars>
  94. <Car>
  95. <StockNumber>1020</StockNumber>
  96. <Make>Nissan</Make>
  97. <Model>Sentra</Model>
  98. </Car>
  99. <Car>
  100. <StockNumber>1010</StockNumber>
  101. <Make>Toyota</Make>
  102. <Model>Corolla</Model>
  103. </Car>
  104. <Car>
  105. <StockNumber>1111</StockNumber>
  106. <Make>Honda</Make>
  107. <Model>Accord</Model>
  108. </Car>
  109. </Cars>
  110. </CarCollection>
  111.  
  112. XmlSerializer ser = new XmlSerializer(typeof(Cars));
  113. Cars cars;
  114. using (XmlReader reader = XmlReader.Create(path))
  115. {
  116. cars = (Cars) ser.Deserialize(reader);
  117. }
  118.  
  119. [Serializable()]
  120. [System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
  121. public class Cars
  122. {
  123. [XmlArrayItem(typeof(Car))]
  124. public Car[] Car { get; set; }
  125. }
  126.  
  127. [Serializable()]
  128. public class Car
  129. {
  130. [System.Xml.Serialization.XmlElement()]
  131. public string StockNumber{ get; set; }
  132.  
  133. [System.Xml.Serialization.XmlElement()]
  134. public string Make{ get; set; }
  135.  
  136. [System.Xml.Serialization.XmlElement()]
  137. public string Model{ get; set; }
  138. }
  139.  
  140. public class Car
  141. {
  142. public string StockNumber { get; set; }
  143. public string Make { get; set; }
  144. public string Model { get; set; }
  145. }
  146.  
  147. [XmlRootAttribute("Cars")]
  148. public class CarCollection
  149. {
  150. [XmlElement("Car")]
  151. public Car[] Cars { get; set; }
  152. }
  153.  
  154. using (TextReader reader = new StreamReader(path))
  155. {
  156. XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));
  157. return (CarCollection) serializer.Deserialize(reader);
  158. }
Add Comment
Please, Sign In to add comment