` <waypoints>
<waypoint>
<properties>
<property value="2.935860e+002" name="RangeAway" />
<property value="2.000000e+000" name="Keel Clearance" />
<property value="1" name="Display Name" />
<property value="" name="OtherCharts" />
<property value="0.000000e+000" name="ROT" />
<property value="0" name="Stop Time" />
<property value="-1.000000e+000" name="TTG" />
<property value="0" name="NavWatchLevel" />
<property value="2.271720e+002" name="BearingTo" />
<property value="" name="BACharts" />
<property value="1" name="TTG Calc" />
<property value="1.000000e+001" name="SOG" />
<property value="0" name="Locked" />
<property value="0" name="Engine Status" />
<property value="1" name="Max Position Fix" />
<property value="0" name="Display Description" />
<property value="5.000000e-001" name="Turn Radius" />
<property value="0" name="Activate" />
<property value="0" name="Type" />
<property value="" name="ReferencePoint" />
<property value="2.935860e+002" name="RangeTo" />
<property value="38 28.346N" name="Latitude" />
<property value="1" name="Leg Type" />
<property value="1.000000e-001" name="Port XTE" />
<property value="WP_001" name="Name" />
<property value="-1.000000e+000" name="TIME Zone" />
<property value="0" name="PilotSign" />
<property value="2.271720e+002" name="BearingAway" />
<property value="51025A43-BBC9-4E95-8E92-84AF4E6AC872" name="ID" />
<property value="" name="PassingTime" />
<property value="0" name="Index" />
<property value="1.000000e-001" name="Stbd XTE" />
<property value="N/A" name="ETA" />
<property value="009 22.978W" name="Longitude" />
</properties>
<description />
</waypoint>
<waypoint>
<properties>
<property value="4.365710e+002" name="RangeAway" />
<property value="2.000000e+000" name="Keel Clearance" />
<property value="1" name="Display Name" />
<property value="" name="OtherCharts" />
<property value="1.909859e+001" name="ROT" />
<property value="0" name="Stop Time" />
<property value="2.935860e+001" name="TTG" />
<property value="0" name="NavWatchLevel" />
<property value="2.131972e+002" name="BearingTo" />
<property value="" name="BACharts" />
<property value="1" name="TTG Calc" />
<property value="1.000000e+001" name="SOG" />
<property value="0" name="Locked" />
<property value="0" name="Engine Status" />
<property value="1" name="Max Position Fix" />
<property value="0" name="Display Description" />
<property value="5.000000e-001" name="Turn Radius" />
<property value="0" name="Activate" />
<property value="0" name="Type" />
<property value="" name="ReferencePoint" />
<property value="4.365710e+002" name="RangeTo" />
<property value="35 08.505N" name="Latitude" />
<property value="1" name="Leg Type" />
<property value="1.000000e-001" name="Port XTE" />
<property value="WP_002" name="Name" />
<property value="-1.000000e+000" name="TIME Zone" />
<property value="0" name="PilotSign" />
<property value="2.131972e+002" name="BearingAway" />
<property value="51025A43-BBC9-4E95-8E92-84AF4E6AC872" name="ID" />
<property value="" name="PassingTime" />
<property value="1" name="Index" />
<property value="1.000000e-001" name="Stbd XTE" />
<property value="01/08 21:11:17(+01)" name="ETA" />
<property value="013 51.175W" name="Longitude" />
</properties>
<description />
</waypoint>
</waypoints>`
我有一个如上所述的文件,我想获取其名称属性包括“纬度”,“经度”,“转半径”,“ BearingTo”,“ RangeTo”的那些Xelements的值,并将这些值分配给不同的值对象的属性。该对象具有类似于name属性值的属性。 该文件有9个节点,因此我应该能够从该文件中生成9个对象。 我已经尝试过
var wayPointCollection = (from property in properties where property.LastAttribute.Value.Equals("Latitude")
select new LocationEntity
{
LATITUDE = (property.FirstAttribute.Value)
}
但是在我的方法中,我只能检查一个属性并获取与其关联的值。如何使用LINQ或Lambda达到目标。
答案 0 :(得分:1)
您可以使用LINQ to XML收集每个航路点,并将其属性转换为Dictionary
,以便检索所需的属性:
var ans = xd.Descendants("waypoint")
.Select(wp => {
var wpd = wp.Descendants("property")
.ToDictionary(wpp => wpp.Attribute("name").Value, wpp => wpp.Attribute("value").Value);
return new LocationEntity {
Latitude = wpd["Latitude"],
Longitude = wpd["Longitude"],
TurnRadius = wpd["Turn Radius"],
BearingTo = wpd["BearingTo"],
RangeTo = wpd["RangeTo"]
};
});
ans
将是一个List<LocationEntity>
,其中包含所有已转换的航点。
或者,您可以创建一个助手类,该助手类搜索属性后代以检索属性值:
public class XMLWaypoint {
IEnumerable<XElement> xmlwp;
public XMLWaypoint(XElement x) {
xmlwp = x.Descendants("property");
}
public string this[string propName]
{
get
{
return xmlwp.FirstOrDefault(wpp => wpp.Attribute("name").Value == propName)?.Attribute("value").Value;
}
}
}
然后,您可以根据需要使用它来查找每个属性:
var ans2 = xd.Descendants("waypoint")
.Select(wp => {
var xwp = new XMLWaypoint(wp);
return new LocationEntity {
Latitude = xwp["Latitude"],
Longitude = xwp["Longitude"],
TurnRadius = xwp["Turn Radius"],
BearingTo = xwp["BearingTo"],
RangeTo = xwp["RangeTo"]
};
});
您还可以创建一种使用Reflection来自动映射到对象中的属性的方法,但这似乎有些矫kill过正,而且还存在属性名称与对象属性名称不匹配的问题(例如“ Turn Radius”)。但是,这确实使我认为,将LINQ扩展为XML的一个有趣的扩展将是XML的实体框架。