具有困难结构的XML的LINQ查询

时间:2015-09-23 12:54:24

标签: c# xml linq

请帮我解析这个XML。我使用LINQ。我的XML:

<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();

我不知道我怎么能得到&#34;走路&#34;并且&#34;通过&#34;在一个LINQ查询中。我只能解析&#34; walk&#34;或者只是&#34;传递&#34;,但我想创建像我的类Path一样的对象。

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

我修正了错别字和错误。我想这就是你真正想要的。

&#13;
&#13;
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; }
    }

}
​
&#13;
&#13;
&#13;