繁体   English   中英

选择XML中的多个元素

[英]Select Multiple Elements in XML

我有以下XML(简化版本)文件:

<Service z:Id="i1" xmlns="http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<routes>
<Route z:Id="i4">
  <timetables>

      <Timetable z:Id="i8">
      <timetableId>11061</timetableId>
      </Timetable>

      <Timetable z:Id="i8">         
      <timetableId>11062</timetableId>   
      </Timetable>

   </timetables>
   </Route>
 </routes>
</Service>

我能够获得第一个ID:11061,但我希望得到第二个ID,在真实文件中还会有其他几个ID。 但是我认为一旦我能得到两个,它将得到大于2的收益。

XDocument doc = XDocument.Load("timetableTest.xml");
        XNamespace ns = "http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary";

        var routeNames = (from n in doc.Descendants(ns + "Service").Descendants(ns + "routes").Descendants(ns + "Route")//.Descendants(ns + "timetables")//.Descendants(ns + "Service")
                          select new RootContainer
                          {
                              Services = (from s in n.Elements(ns + "timetables")//.Elements(ns + "clients")
                                                                              // where n.Elements(ns + "Service") != null
                                          select new Services

                                          {
                                              ServiceName = s.Element(ns + "Timetable").Element(ns + "timetableId").Value,
                                              //serviceIconUrl = "/Assets/Services/" + s.Element(ns + "serviceName").Value + ".png",
                                             // ServiceId = s.Element(ns + "serviceId").Value
                                          }).ToList()
                          }).Single();

        listServices.ItemsSource = routeNames.Services;

为了获得多个时间表ID,我需要更改什么?

更新:我怎么做,但有两条路线? 只是重新查看了原始的XML提要。

<Service z:Id="i1" xmlns="http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<routes>
<Route z:Id="i4">
  <timetables>

      <Timetable z:Id="i8">
      <timetableId>11061</timetableId>
      </Timetable>

      <Timetable z:Id="i8">         
      <timetableId>11062</timetableId>   
      </Timetable>

   </timetables>
   </Route>
 <Route z:Id="i4">
  <timetables>

      <Timetable z:Id="i8">
      <timetableId>11061</timetableId>
      </Timetable>

      <Timetable z:Id="i8">         
      <timetableId>11062</timetableId>   
      </Timetable>

   </timetables>
   </Route>

 </routes>
 </Service>

您需要一个复合from子句来选择许多时间表:

XDocument doc = XDocument.Load("timetableTest.xml");
    XNamespace ns = "http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary";

    var routeNames = (from n in doc.Descendants(ns + "Service").Descendants(ns + "routes").Descendants(ns + "Route")//.Descendants(ns + "timetables")//.Descendants(ns + "Service")
                      select new 
                      {
                          Services = (from s in n.Elements(ns + "timetables")//.Elements(ns + "clients")
                                      from t in s.Descendants(ns + "Timetable")                              // where n.Elements(ns + "Service") != null
                                      select new 

                                      {
                                          ServiceName = t.Element(ns + "timetableId").Value,
                                          //serviceIconUrl = "/Assets/Services/" + s.Element(ns + "serviceName").Value + ".png",
                                         // ServiceId = s.Element(ns + "serviceId").Value
                                      }).ToList()
                      }).Single();

    listServices.ItemsSource = routeNames.Services;

在内部for循环RootContainer类中使用后代

Services = (from s in n.Elements(ns + "timetables").Descendants(ns +"Timetable") 

并直接在元素中访问

ServiceName = s.Element(ns + "timetableId").Value,

完整的代码

var routeNames = (from n in doc.Descendants(ns + "Service").Descendants(ns + "routes").Descendants(ns + "Route")//.Descendants(ns + "timetables")//.Descendants(ns + "Service")
                          select new RootContainer
                          {
                              Services = (from s in n.Elements(ns + "timetables").Descendants(ns +"Timetable")
                                                                              // where n.Elements(ns + "Service") != null
                                          select new Services

                                          {
                                              ServiceName = s.Element(ns + "timetableId").Value,
                                              //serviceIconUrl = "/Assets/Services/" + s.Element(ns + "serviceName").Value + ".png",
                                             // ServiceId = s.Element(ns + "serviceId").Value
                                          }).ToList()
                          }).Single();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM