Question

This is the sample XML that I have got:

<ArrayOfService >
  <Service z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
          <primaryUrl>http://www.buses.co.uk/1A</primaryUrl>
          <relativePosition>2</relativePosition>
          <serviceDescription>Whitehawk - County Hospital - City Centre - Hove - Portslade - Mile Oak</serviceDescription>
          <serviceId>1150</serviceId>
          <serviceName>1A</serviceName>
          <serviceNotes />
          <serviceText>Whitehawk - Mile Oak</serviceText>
          <serviceUrls />
     </Service>
</ArrayOfService>

This is the code:

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

var list = (from item in loadedData.Descendants("ArrayOfService").Descendants("Service")
            where (string)item.Element("serviceName") == "1A"
            select (string)item.Element("serviceId")).FirstOrDefault();

The result is coming out as null, why is this?

Était-ce utile?

La solution 2

I could not find the problem, but I just copied the contents of the xml into another file and this then worked.

Autres conseils

loadedData.Descendants("ArrayOfService") should be loadedData.Root

See XDocument.Root (MSDN) for details.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top