marcusstarnes
marcusstarnes

Reputation: 6531

Deserialize nested XML element into class in C#

I have the following XML structure (edited for brevity) which I have no control over.

<GetVehicles>
    <ApplicationArea>
        <Sender>
            <Blah></Blah>
        </Sender>
    </ApplicationArea>
    <DataArea>
        <Error>
            <Blah></Blah>
        </Error>
        <Vehicles>
            <Vehicle>
                <Colour>Blue</Colour>
                <NumOfDoors>3</NumOfDoors>
                <BodyStyle>Hatchback</BodyStyle>
            <Vehicle>
        </Vehicles>
    </DataArea>
</GetVehicles>

I have the following Class:

[XmlRoot("GetVehicles"), XmlType("Vehicle")]
public class Vehicle
{
    public string Colour { get; set; }
    public string NumOfDoors { get; set; }
    public string BodyStyle { get; set; }
}

I want to be able to deserialize the XML into a single instance of this Vehicle class. 99% of the time, the XML should only return a single 'Vehicle' element. I'm not yet dealing with it yet if it contains multiple 'Vehicle' elements inside the 'Vehicles' element.

Unfortunately, the XML data isn't currently being mapped to my class properties; they are being left blank upon calling my Deserialize method.

For completeness, here is my Deserialize method:

private static T Deserialize<T>(string data) where T : class, new()
{
    if (string.IsNullOrEmpty(data))
        return null;

    var ser = new XmlSerializer(typeof(T));

    using (var sr = new StringReader(data))
    {
        return (T)ser.Deserialize(sr);
    }
}

I don't care about the other more parent elements such as 'ApplicationArea', 'Error' etc. I am only interesting in extracting the data within the 'Vehicle' element. How do I get it to only deserialize this data from the XML?

Upvotes: 15

Views: 22127

Answers (2)

Jon Egerton
Jon Egerton

Reputation: 41549

Building on Ilya's answer:

This can be optimized slightly, since there is already a loaded node: there is no need to pass the xml down to a string (using vehicle.ToString()), only to cause the serializer to have to re-parse it (using a StringReader).

Instead, we can created a reader directly using XNode.CreateReader:

private static T Deserialize<T>(XNode data) where T : class, new()
{
    if (data == null)
        return null;

    var ser = new XmlSerializer(typeof(T));
    return (T)ser.Deserialize(data.CreateReader());
}

Or if data is a XmlNode, use a XmlNodeReader:

private static T Deserialize<T>(XmlNode data) where T : class, new()
{
    if (data == null)
        return null;

    var ser = new XmlSerializer(typeof(T));
    using (var xmlNodeReader = new XmlNodeReader(data))
    {
        return (T)ser.Deserialize(xmlNodeReader);
    }
}

We can then use:

var vehicle = XDocument.Parse(xml)
                       .Descendants("Vehicle")
                       .First();

Vehicle v = Deserialize<Vehicle>(vehicle);

Upvotes: 12

Ilya Ivanov
Ilya Ivanov

Reputation: 23626

I'm not aware of the full context of your problem, so this solution might not fit into your domain. But one solution is to define your model as:

[XmlRoot("Vehicle")] //<-- optional
public class Vehicle
{
    public string Colour { get; set; }
    public string NumOfDoors { get; set; }
    public string BodyStyle { get; set; }
}

and pass specific node into Deserialize method using LINQ to XML:

var vehicle = XDocument.Parse(xml)
                       .Descendants("Vehicle")
                       .First();

Vehicle v = Deserialize<Vehicle>(vehicle.ToString());


//display contents of v 
Console.WriteLine(v.BodyStyle);   //prints Hatchback
Console.WriteLine(v.Colour);      //prints Blue
Console.WriteLine(v.NumOfDoors);  //prints 3

Upvotes: 9

Related Questions