0

我有 ac#MVC 模型,我想将其序列化为 XML 文档,如下所示:

<Vehicle>
    <Type color="red" speed="50mph">Ford</Type>
    <Type color="blue" speed="70mph">Toyota</Type>
</Vehicle>

这是模型:

[Serializable]
public class Production
{
    public List<Vehicle> Vehicles { get; set; }
}

[Serializable]
public class Vehicle
{
    [XmlAttribute]
    public string color { get; set; }

    [XmlAttribute]
    public string speed { get; set; }
}

由于类 Vehicle 不是属性,我需要向类添加什么以赋予它例如“福特”或“丰田”的值

现在我有:

var myvehicle = new Vehicle {color = "red", speed = "50mph"};
4

1 回答 1

1

添加另一个具有该属性的[XmlText]属性:

[XmlText]
public string Make {get; set;}

您还应该将属性添加到您的列表中Vehicles

[XmlArray("Vehicle")]
[XmlArrayItem("Type")]
public List<Vehicle> Vehicles { get; set; }
于 2013-08-27T01:45:22.100 回答