c # - Vælg flere elementer i XML

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg har følgende XML-fil (cut down version):


<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>


Jeg er i stand til at få det første ID: 11061, men jeg ønsker at få den anden, i den rigtige fil vil der være flere andre. Men jeg antager, at når jeg kan få to, bliver det mere end 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;


Hvad skal jeg ændre for at få flere timetables Ids?


Opdatering: Hvordan gør jeg det samme, men med to ruter? Bare genkig på den oprindelige xml feed.


<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>

Bedste reference


Du har brug for en sammensætning fra klausul for at vælge mange fartplaner: [8]


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;

Andre referencer 1


Brug efterkommere i RootContainer-klassen


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


og få adgang direkte i elementet


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


fuld kode


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