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.

3
  • possible duplicate of Deserialize XML Array Where Root is Array and Elements Dont Follow Conventions Commented Jul 27, 2015 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. Commented Jul 27, 2015 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 Commented Jul 27, 2015 at 14:01

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Erik for the quick and perfect answer.
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>

1 Comment

I did it, but didn't suceed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.