Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <a href="http://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend.aspx" rel="noreferrer">function</a> reads the whole stream and returns a string, so the Deserialze() 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 :).</p> <p>Here are the classes:</p> <pre><code>[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; } } </code></pre> <p>The Deserialize function:</p> <pre><code>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(); </code></pre> <p>And the slightly tweaked xml (I needed to add a new element to wrap &lt;Cars&gt;...Net is picky about deserializing arrays):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;CarCollection&gt; &lt;Cars&gt; &lt;Car&gt; &lt;StockNumber&gt;1020&lt;/StockNumber&gt; &lt;Make&gt;Nissan&lt;/Make&gt; &lt;Model&gt;Sentra&lt;/Model&gt; &lt;/Car&gt; &lt;Car&gt; &lt;StockNumber&gt;1010&lt;/StockNumber&gt; &lt;Make&gt;Toyota&lt;/Make&gt; &lt;Model&gt;Corolla&lt;/Model&gt; &lt;/Car&gt; &lt;Car&gt; &lt;StockNumber&gt;1111&lt;/StockNumber&gt; &lt;Make&gt;Honda&lt;/Make&gt; &lt;Model&gt;Accord&lt;/Model&gt; &lt;/Car&gt; &lt;/Cars&gt; &lt;/CarCollection&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload