[英]how to search for a xml node value, then create new attribute for that element in c#
[英]How to create C# MVC model XML element with attribute and value
我有一個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類別不是屬性,因此我需要在該類別中添加一個值,例如“ ford”或“ Toyota”
現在我有:
var myvehicle = new Vehicle {color = "red", speed = "50mph"};
使用[XmlText]
屬性添加另一個屬性:
[XmlText]
public string Make {get; set;}
您還應該將屬性添加到“ Vehicles
列表中:
[XmlArray("Vehicle")]
[XmlArrayItem("Type")]
public List<Vehicle> Vehicles { get; set; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.