将嵌套的XML元素反序列化为C#中的类 [英] Deserialize nested XML element into class in C#

查看:134
本文介绍了将嵌套的XML元素反序列化为C#中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下XML结构(为简便起见进行了编辑),我无法控制。

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>

我有以下课程:

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

我希望能够将XML反序列化为一个单独的实例车辆类别。 XML在99%的时间中仅应返回一个 Vehicle元素。如果它在'Vehicles'元素中包含多个'Vehicle'元素,我还没有处理。

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.

不幸的是,当前未映射XML数据。对我的班级财产;

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

为了完整起见,这是我的Deserialize方法:

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);
    }
}

我不在乎其他父元素例如 ApplicationArea, Error等。我仅对提取 Vehicle元素中的数据很感兴趣。如何仅从XML反序列化数据?

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?

推荐答案

我不知道您的完整上下文问题,因此此解决方案可能不适合您的域。但是一种解决方案是将模型定义为:

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; }
}

并将特定节点传递到 Deserialize 方法:

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

这篇关于将嵌套的XML元素反序列化为C#中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆