我有一个List<NXRoute>
,每个NXRoute
还包含一个List<Path>
有我的班级定义:
[XmlRoot("NXRoutes")]
public class NXRoutes
{
[XmlElement("NXRoute")]
public List<NXRoute> NXRoute { get; set; }
}
public class NXRoute
{
[XmlAttribute("ID")]
public string ID { get; set; }
[XmlAttribute("OriginSignal")]
public string OriginSignal { get; set; }
[XmlAttribute("DestinationSignal")]
public string DestinationSignal { get; set; }
[XmlElement("Path")]
public List<Path> Path { get; set; }
}
public class Path
{
[XmlAttribute("ID")]
public string ID { get; set; }
[XmlAttribute("Preferred")]
public string Preferred { get; set; }
[XmlAttribute("SnowPlan")]
public string SnowPlan { get; set; }
[XmlText]
public string PathInnerText { get; set; }
}
我将在XmlSerialization中使用它们来生成xml文件
为了给属性(ID,Preferred ... etc)分配值, 如何到达“路径”列表的元素?
答案 0 :(得分:0)
您可以遍历集合以访问每个Path
实例
访问值或在现有实例上设置值
NXRoutes routes = new NXRoutes();
foreach (var route in routes.NXRoute)
{
foreach (var path in route.Path)
{
path.ID = 5;
path.Preferred = "preferred";
path.SnowPlan = "plan"
}
}
设置新实例的值
NXRoutes routes = getRoutes();
routes.NXRoute = new List<NXRoute>()
{
new NXRoute { ID = 1, OrginSignal = "value", Path = new List<Path>()
{
new Path { ID = 1, Preferred = "value", SnowPlan = "plan" },
new Path { ID = 2, Preferred = "value", SnowPlan2 = "plan2" }
}
},
new NXRoute { ID = 2, OrginSignal = "value" Path = new List<Path>()
{
new Path { ID = 3, Preferred = "value", SnowPlan3 = "plan4" },
new Path { ID = 4, Preferred = "value", SnowPlan4 = "plan4" }
},
}
}