Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <Cars>
- <Car>
- <StockNumber>1020</StockNumber>
- <Make>Nissan</Make>
- <Model>Sentra</Model>
- </Car>
- <Car>
- <StockNumber>1010</StockNumber>
- <Make>Toyota</Make>
- <Model>Corolla</Model>
- </Car>
- <Car>
- <StockNumber>1111</StockNumber>
- <Make>Honda</Make>
- <Model>Accord</Model>
- </Car>
- </Cars>
- [Serializable()]
- public class Car
- {
- [System.Xml.Serialization.XmlElementAttribute("StockNumber")]
- public string StockNumber{ get; set; }
- [System.Xml.Serialization.XmlElementAttribute("Make")]
- public string Make{ get; set; }
- [System.Xml.Serialization.XmlElementAttribute("Model")]
- public string Model{ get; set; }
- }
- [System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
- public class Cars
- {
- [XmlArrayItem(typeof(Car))]
- public Car[] Car { get; set; }
- }
- public class CarSerializer
- {
- public Cars Deserialize()
- {
- Cars[] cars = null;
- string path = HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data/") + "cars.xml";
- XmlSerializer serializer = new XmlSerializer(typeof(Cars[]));
- StreamReader reader = new StreamReader(path);
- reader.ReadToEnd();
- cars = (Cars[])serializer.Deserialize(reader);
- reader.Close();
- return cars;
- }
- }
- [Serializable()]
- public class Car
- {
- [System.Xml.Serialization.XmlElement("StockNumber")]
- public string StockNumber { get; set; }
- [System.Xml.Serialization.XmlElement("Make")]
- public string Make { get; set; }
- [System.Xml.Serialization.XmlElement("Model")]
- public string Model { get; set; }
- }
- [Serializable()]
- [System.Xml.Serialization.XmlRoot("CarCollection")]
- public class CarCollection
- {
- [XmlArray("Cars")]
- [XmlArrayItem("Car", typeof(Car))]
- public Car[] Car { get; set; }
- }
- CarCollection cars = null;
- string path = "cars.xml";
- XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));
- StreamReader reader = new StreamReader(path);
- cars = (CarCollection)serializer.Deserialize(reader);
- reader.Close();
- <?xml version="1.0" encoding="utf-8"?>
- <CarCollection>
- <Cars>
- <Car>
- <StockNumber>1020</StockNumber>
- <Make>Nissan</Make>
- <Model>Sentra</Model>
- </Car>
- <Car>
- <StockNumber>1010</StockNumber>
- <Make>Toyota</Make>
- <Model>Corolla</Model>
- </Car>
- <Car>
- <StockNumber>1111</StockNumber>
- <Make>Honda</Make>
- <Model>Accord</Model>
- </Car>
- </Cars>
- </CarCollection>
- XmlSerializer ser = new XmlSerializer(typeof(Cars));
- Cars cars;
- using (XmlReader reader = XmlReader.Create(path))
- {
- cars = (Cars) ser.Deserialize(reader);
- }
- [Serializable()]
- [System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
- public class Cars
- {
- [XmlArrayItem(typeof(Car))]
- public Car[] Car { get; set; }
- }
- [Serializable()]
- public class Car
- {
- [System.Xml.Serialization.XmlElement()]
- public string StockNumber{ get; set; }
- [System.Xml.Serialization.XmlElement()]
- public string Make{ get; set; }
- [System.Xml.Serialization.XmlElement()]
- public string Model{ get; set; }
- }
- public class Car
- {
- public string StockNumber { get; set; }
- public string Make { get; set; }
- public string Model { get; set; }
- }
- [XmlRootAttribute("Cars")]
- public class CarCollection
- {
- [XmlElement("Car")]
- public Car[] Cars { get; set; }
- }
- using (TextReader reader = new StreamReader(path))
- {
- XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));
- return (CarCollection) serializer.Deserialize(reader);
- }
Add Comment
Please, Sign In to add comment