Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 616
marcusstarnes Avatar asked May 15 '13 10:05

marcusstarnes


2 Answers

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);
like image 108
Jon Egerton Avatar answered Nov 02 '22 18:11

Jon Egerton


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
like image 20
Ilya Ivanov Avatar answered Nov 02 '22 19:11

Ilya Ivanov