2015-09-23 33 views
1

請幫我解析這個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查詢中獲得「散步」和「通過」。我只能解析「走路」或只能「通過」,但我想創建像我的類路徑對象。

+0

應該如何'PathActions'填充了'pass'?例如,「停止」在哪裏去? 「ToStop」和「FromStop」應該是什麼? –

+0

你是說你想要一個返回「Walk」和「Pass」對象的Linq查詢作爲「PathActions?」返回嗎? –

+0

僅供參考,認爲使用'int'和'string'代替'Int32'和'String'是最好的做法。 – juharr

回答

2

你可以做

xDoc.Elements("path") 
    .Elements("actions") 
    .Elements() 
    .Where(e => e.Name == "walk" || e.Name == "pass") 

代替。

當然在你的查詢中res已經是xDoc.Elements("path")中的一個元素,所以你真的應該使用它來確保你只添加與當前路徑元素相關的動作。

ActionsList = (from nextRes in res.Elements("actions") 
            .Elements() 
            .Where(e => e.Name == "walk" || e.Name == "pass") 
       select ...).ToList() 
0

我修正了拼寫錯誤和錯誤。我認爲這是你真正想要的。

using System; 
 
using System.Collections.Generic; 
 
using System.Linq; 
 
using System.Text; 
 
using System.Xml; 
 
using System.Xml.Linq; 
 

 
namespace ConsoleApplication1 
 
{ 
 
    class Program 
 
    { 
 
     static void Main(string[] args) 
 
     { 
 
      string response = 
 
        "<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>"; 
 

 

 

 

 
      var xDoc = XDocument.Parse(response); 
 

 

 
      Data.Path path = xDoc.Elements("path").Select(res => new Data.Path() { 
 
            Time = (Int32?)res.Element("time"), 
 
            Price = (Int32?)res.Element("price"), 
 
            Length = (Int32?)res.Element("length"), 
 
            TransportTakes = (Int32?)res.Element("transportTakes"), 
 
            ActionsList = res.Elements("actions").Elements().Select(nextRes => 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(), 
 
           }).FirstOrDefault(); 
 
     } 
 
    } 
 
    public static class Data 
 
    { 
 
     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 string XName { get; set; } 
 
     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; } 
 
    } 
 

 
} 
 
​