Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I am consuming SOAP services from my application and I get following XMl content as response consuming .wsdl file in my code.

<Vehicles>
    <FourWheeler>
        <Availability>
            <Brand>Toyota</Brand>
            <Model>Fortuner</Model>
            <Country>Japan</Country>
            <Cost>90000</Cost>           
        </Availability>
        <Availability>
            <Brand>Hyundai</Brand>
            <Model>Elentra</Model>
            <Country>South Korea</Country>
            <Cost>75000</Cost>           
        </Availability>
        <Availability>
            <Brand>Volkswagan</Brand>
            <Model>Polo</Model>
            <Country>Gremany</Country>
            <Cost>95000</Cost>                   
        </Availability>
        <Availability>
            <Brand>Tata</Brand>
            <Model>Nano</Model>
            <Country>25000</Country>
            <Cost></Cost>                    
        </Availability>
    </Outbound>
    <TwoWheeler></TwoWheeler>
</Vehicles>


I have designed my class to deserialise XML this way::

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

using System.Xml.Serialization;
namespace ConsoleApp2
{
    [XmlRoot("Vehicles")]
    public class Vehicles
    {
        [XmlElement("FourWheeler")]
        public FourWheeler FourWheeler
        {
            get;
            set;
        }
    }

    [XmlRoot("FourWheeler")]
    public class FourWheeler
    {
        [XmlElement("Availability")]
        public Vehicles Availability
        {
            get;
            set;
        }
    }

    [XmlRoot("Availability")]
    public class Availability
    {

        [XmlElement("Brand")]
        public string Brand
        {
            get;
            set;
        }

        [XmlElement("Model")]
        public string Model
        {
            get;
            set;
        }


        [XmlElement("Country")]
        public string Country
        {
            get;
            set;
        }

        [XmlElement("Cost")]
        public string Cost
        {
            get;
            set;
        }
    }
}


My code to deserialize XML to list is :

XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Vehicles>));
System.IO.StreamReader sr = new System.IO.StreamReader("E:\\xmlTier.txt");
List<Vehicles> flightavailabilitylst = (List<Vehicles>)xmlSerializer.Deserialize(sr);


I am getting error in line
List<Vehicles> flightavailabilitylst = (List<Vehicles>)xmlSerializer.Deserialize(sr);
:
System.InvalidOperationException: 'There is an error in XML document (1, 2).'

InvalidOperationException: <Vehicles xmlns=''> was not expected.


What is happening wrong and what is right approach to convert XML into list of object???

What I have tried:

Looking into XML, hierarchy is Vehicles > FourWheeler > List of Availability... So I added code:
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "Vehicles";
xRoot.IsNullable = true;


This did not help me.
Posted
Updated 12-Jan-23 0:01am

<Vehicles>
    <FourWheeler>
        <Availability>
            <Brand>Toyota</Brand>
            <Model>Fortuner</Model>
            <Country>Japan</Country>
            <Cost>90000</Cost>           
        </Availability>
        <Availability>
            <Brand>Hyundai</Brand>
            <Model>Elentra</Model>
            <Country>South Korea</Country>
            <Cost>75000</Cost>           
        </Availability>
        <Availability>
            <Brand>Volkswagan</Brand>
            <Model>Polo</Model>
            <Country>Gremany</Country>
            <Cost>95000</Cost>                   
        </Availability>
        <Availability>
            <Brand>Tata</Brand>
            <Model>Nano</Model>
            <Country>25000</Country>
            <Cost></Cost>                    
        </Availability>
    </FourWheeler>
    <TwoWheeler></TwoWheeler>
</Vehicles>


XML updated. but still showing same error.
 
Share this answer
 
Your "FourWheeler" and "Outbound" tags are "not matched".

i.e. your XML is invalid.
 
Share this answer
 
Comments
Codes DeCodes 7-Mar-19 12:15pm    
XML updated. see below. Still showing same error
[no name] 7-Mar-19 13:00pm    
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
[System.Xml.Serialization.XmlRootAttribute( Namespace = "", IsNullable = false )]
public partial class Vehicles {

private VehiclesAvailability[] fourWheelerField;

private object twoWheelerField;

/// <remarks>
[System.Xml.Serialization.XmlArrayItemAttribute( "Availability", IsNullable = false )]
public VehiclesAvailability[] FourWheeler {
get {
return this.fourWheelerField;
}
set {
this.fourWheelerField = value;
}
}

/// <remarks>
public object TwoWheeler {
get {
return this.twoWheelerField;
}
set {
this.twoWheelerField = value;
}
}
}

/// <remarks>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
public partial class VehiclesAvailability {

private string brandField;

private string modelField;

private string countryField;

private string costField;

/// <remarks>
public string Brand {
get {
return this.brandField;
}
set {
this.brandField = value;
}
}

/// <remarks>
public string Model {
get {
return this.modelField;
}
set {
this.modelField = value;
}
}

/// <remarks>
public string Country {
get {
return this.countryField;
}
set {
this.countryField = value;
}
}

/// <remarks>
public string Cost {
get {
return this.costField;
}
set {
this.costField = value;
}
}
}
Your xml isn't valid. Replace

</Outbound>
<TwoWheeler></TwoWheeler>


with

</FourWheeler>


The XML represents a single Vehicles node not a list of them so change the code accordingly

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Vehicles));
System.IO.StreamReader sr = new System.IO.StreamReader( ... );

Vehicles flightavailabilitylst = (Vehicles)xmlSerializer.Deserialize(sr);


Also Availability is a list of Available objects, not a single Vehicles object so change that also

[XmlRoot("FourWheeler")]
public class FourWheeler
{
    [XmlElement("Availability")]
    public List<Availability> Availability
    {
        get;
        set;
    }
}
 
Share this answer
 
v2
Comments
Codes DeCodes 7-Mar-19 12:15pm    
XML updated. see below. Still showing same error
F-ES Sitecore 7-Mar-19 12:42pm    
I've updated my solution
Codes DeCodes 7-Mar-19 13:09pm    
Thanks for investing your time in this.. It worked... Thanks Man..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900