Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Deserialize XML document

How do I Deserialize this XML document:

<?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> 

I have this:

[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;     } } 

that don't seem to work :-(

like image 825
Alex Avatar asked Dec 12 '08 21:12

Alex


People also ask

What is the correct way of using XML deserialization?

The most common usage of XML serialization is when XML data is sent from the database server to the client. Implicit serialization is the preferred method in most cases because it is simpler to code, and sending XML data to the client allows the Db2 client to handle the XML data properly.


2 Answers

How about you just save the xml to a file, and use xsd to generate C# classes?

  1. Write the file to disk (I named it foo.xml)
  2. Generate the xsd: xsd foo.xml
  3. Generate the C#: xsd foo.xsd /classes

Et voila - and C# code file that should be able to read the data via XmlSerializer:

    XmlSerializer ser = new XmlSerializer(typeof(Cars));     Cars cars;     using (XmlReader reader = XmlReader.Create(path))     {         cars = (Cars) ser.Deserialize(reader);     } 

(include the generated foo.cs in the project)

like image 51
Marc Gravell Avatar answered Nov 13 '22 21:11

Marc Gravell


Here's a working version. I changed the XmlElementAttribute labels to XmlElement because in the xml the StockNumber, Make and Model values are elements, not attributes. Also I removed the reader.ReadToEnd(); (that function reads the whole stream and returns a string, so the Deserialize() function couldn't use the reader anymore...the position was at the end of the stream). I also took a few liberties with the naming :).

Here are the classes:

[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; } } 

The Deserialize function:

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(); 

And the slightly tweaked xml (I needed to add a new element to wrap <Cars>...Net is picky about deserializing arrays):

<?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> 
like image 42
Kevin Tighe Avatar answered Nov 13 '22 21:11

Kevin Tighe