-1

I have xml document copied from here How to Deserialize XML document

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

I use that code with class generated from /paste special/paste xml as classes

using System;
using System.IO;
using System.Xml.Serialization;

namespace DeSerialize
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlSerializer serializer =
        new XmlSerializer(typeof(CarCollectionCar));

            // Declare an object variable of the type to be deserialized.
            CarCollectionCar i;

            using (Stream reader = new FileStream("cars.xml", FileMode.Open))
            {
                // Call the Deserialize method to restore the object's state.
                i = (CarCollectionCar)serializer.Deserialize(reader);
            }

            // Write out the properties of the object.
            Console.Write(
            // i.StockNumber + "\t" +
            i.StockNumber + "\t" +
            //i.StockNumber + "\t" +
            i.Model + "\t" +
            i.Make);


        }
    }


    [Serializable()]
    public partial class CarCollection
    {

        /// <remarks/>
        [XmlArrayItem("Car", IsNullable = false)]
        public CarCollectionCar[] Cars { get; set; }
    }

    /// <remarks/>
    [Serializable()]
    [System.ComponentModel.DesignerCategory("code")]
    [XmlType(AnonymousType = true)]
    public partial class CarCollectionCar
    {

        /// <remarks/>
        public ushort StockNumber { get; set; }

        /// <remarks/>
        public string Make { get; set; }

        /// <remarks/>
        public string Model { get; set; }
    }


}

I get error

Unhandled Exception: System.InvalidOperationException: There is an error in XML 
document (2, 2). ---> System.InvalidOperationException: <CarCollection xmlns=''> 
was not expected.
       at 
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderCarCollectionCar.Read3_CarCollectionCar()
       --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
   at DeSerialize.Program.Main(String[] args)

How to resolve problem and output needed Cars params?

Taterhead
  • 5,763
  • 4
  • 31
  • 40
streamc
  • 676
  • 3
  • 11
  • 27
  • Here is a pro tip. Try to serialize your class first. Then see what is the issue with deserialization. – Gilad Nov 16 '19 at 20:37
  • 1
    `new XmlSerializer(typeof(CarCollectionCar));` should be `new XmlSerializer(typeof(CarCollection));` as your are deserializing a collection of cars, not an individual car. – Eugene Podskal Nov 16 '19 at 21:24

1 Answers1

-2

It's because it can't understand CarCollection on line two of your XML.

Object is CarCollection.Cars[Car]

Best way might be to build an object of car collections and serialise this into XML and see what it outputs from there. Once you can serialise it to xml then deserialising into objects is fairly easy.

Or you could deserialise into a dynamic C# object and see what object it builds.

But this code will work to deserialise

This code will work though as objects match the xml

class Program
{
    static void Main(string[] args)
    {
        XmlSerializer serializer =
    new XmlSerializer(typeof(CarCollection));

        // Declare an object variable of the type to be deserialized.
        CarCollection i;

        using (Stream reader = new FileStream("cars.xml", FileMode.Open))
        {
            // Call the Deserialize method to restore the object's state.
            i = (CarCollection)serializer.Deserialize(reader);
        }

        // Write out the properties of the object.
       // Console.Write(
        // i.StockNumber + "\t" +
       /// i.StockNumber + "\t" +
        //i.StockNumber + "\t" +
       // i.Model + "\t" +
       // i.Make);

    }
}

[Serializable()]
public partial class CarCollection
{

    /// <remarks/>
    [XmlArrayItem("Car", IsNullable = false)]
    public Car[] Cars { get; set; }
}

/// <remarks/>
[Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[XmlType(AnonymousType = true)]
public partial class Car
{
    /// <remarks/>
    public ushort StockNumber { get; set; }

    /// <remarks/>
    public string Make { get; set; }

    /// <remarks/>
    public string Model { get; set; }
}
haldo
  • 14,512
  • 5
  • 46
  • 52
fkerrigan
  • 270
  • 2
  • 9
  • I could not deserialize caus I already deserialize with C# tool. I need to use it and not deserialize 1000 times a day. – streamc Nov 17 '19 at 06:09