0

I'm trying to deseaqralize this XML document into a list of Car objects, but it is coming up null.

Here's a sample XML document based on this post: How to Deserialize XML document

<?xml version="1.0" encoding="utf-8"?>
<Cars>
  <car id="1">
    <StockNumber>1020</StockNumber>
    <Make>Nissan</Make>
    <Model>Sentra</Model>
  </car>
  <car id="2">
    <StockNumber>1010</StockNumber>
    <Make>Toyota</Make>
    <Model>Corolla</Model>
  </car>
  <car id="3">
    <StockNumber>1111</StockNumber>
    <Make>Honda</Make>
    <Model>Accord</Model>
  </car>
</Cars>

Required Classes:

[Serializable()]
public class Car
{
    [System.Xml.Serialization.XmlAttribute("id")]
    public int id { get; set; }

    [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("Cars")]
public class Cars
{
    [XmlArray("Cars")]
    [XmlArrayItem("Car", typeof(Car))]
    public List<Car> Car { get; set; }        
}

Deserialize function:

public void ParseReturnXmlForVirtualEvent2()
{
    Cars cars = null;
    string path = @"E:\Projects\Newcars.xml";

    XmlDocument pdoc = new XmlDocument();
    pdoc.Load(path);
    XDocument Doc = new XDocument();
    Doc = XDocument.Parse(pdoc.OuterXml);            

    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Cars));

    System.Xml.XmlReader reader = Doc.CreateReader();
    cars = (Cars)serializer.Deserialize(reader);
    reader.Close();

    //return cars;
}

Let me know if you need me to provide further details.

Community
  • 1
  • 1
Vikrant
  • 147
  • 1
  • 10
  • possible duplicate of [Deserialize XML Array Where Root is Array and Elements Dont Follow Conventions](http://stackoverflow.com/questions/15258818/deserialize-xml-array-where-root-is-array-and-elements-dont-follow-conventions) – Evan Mulawski Jul 27 '15 at 13:18
  • Why are you loading as an `XmlDocument` and then using that to parse an `XDocument` and then using that to create an `XmlReader`? Just create an `XmlReader` using `XmlReader.Create(path)`. You are parsing into two unrelated XML DOM's and throwing away. – Charles Mager Jul 27 '15 at 13:25
  • I agree with you Charles, but its mandotry for me to use Xmldocument and XDocument for other code purpose. but my query is to get list of Cars – Vikrant Jul 27 '15 at 14:01

2 Answers2

2

You can fix this by replacing the Car property with:

[XmlElement("car", typeof(Car))]
public List<Car> Car { get; set; }

Your code had three problems:

  1. You specified "Car" as the element's name but it actually is "car".
  2. The Car property should not be decorated with [XmlArray("Cars")], as the "Cars" XML element is already defined on the Cars class itself.
  3. The Car property should be defined using XmlElement and not XmlArrayItem.
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
1

You need to capitalize car in your xml file. The element should look like this:

<Car id="1">
  <StockNumber>1020</StockNumber>
  <Make>Nissan</Make>
  <Model>Sentra</Model>
</Car>