請幫我解析這個XML。我爲此使用LINQ。我的XML:帶有困難結構的XML的LINQ查詢
<path>
<time>1234</time>
<price>40</price>
<length>7680</length>
<transportTakes>2</transportTakes>
<actions>
<walk>
<length>50</length>
<toStop>24</toStop>
<comment>Information</comment>
</walk>
<pass>
<length>2350</length>
<stops>24,785,234,644,53,89</stops>
<routes>67,46,275,365,24</routes>
<comment>Information</comment>
</pass>
<actions>
<path>
我也有類:
public class Path
{
public Int32 Time { get; set; }
public Int32 Price { get; set; }
public Int32 Length { get; set; }
public Int32 TransportTakes { get; set; }
public List<PathActions> ActionsList { get; set; }
}
和類:
public class PathActions
{
public Int32 LengthActions { get; set; }
public Int32 ToStop { get; set; }
public String Routes { get; set; }
public Int32 FromStop { get; set; }
public string Comment { get; set; }
}
現在,我的方法是這樣的:
var xDoc = XDocument.Parse(response);
Data.Path path = (from res in xDoc.Elements("path")
select new Data.Path()
{
Time = (Int32)res.Element("time"),
Price = (Int32)res.Element("price"),
Length = (Int32)res.Element("length"),
TransportTakes = (Int32)res.Element("transpotTakes"),
ActionsList = (from nextRes in xDoc.Elements("path")
.Elements("actions")
.Elements("walk")
select new PathActions()
{
XName = (String)nextRes.Name.LocalName,
LengthActions = (Int32)nextRes.Element("length"),
ToStop = (Int32)nextRes.Element("toStop"),
Routes = (String)nextRes.Element("routes"),
FromStop = (Int32)nextRes.Element("fromStop"),
Comment = (String)nextRes.Element("comment")
}).ToList()
}).Single();
我不知道我如何在一個LINQ查詢中獲得「散步」和「通過」。我只能解析「走路」或只能「通過」,但我想創建像我的類路徑對象。
應該如何'PathActions'填充了'pass'?例如,「停止」在哪裏去? 「ToStop」和「FromStop」應該是什麼? –
你是說你想要一個返回「Walk」和「Pass」對象的Linq查詢作爲「PathActions?」返回嗎? –
僅供參考,認爲使用'int'和'string'代替'Int32'和'String'是最好的做法。 – juharr