Linq2Xml - how to get nodes and childnodes related

Iturdu

New Member
I have xml like this:\[code\]<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body> <supplyCrew xmlns="http://site.ddf.com"> <login> <login>XXXX</login> <password>XXXX</password> </login> <flightInformation> <flights> <item> <arrivalDateTime>2010-11-08T22:48:00.000Z</arrivalDateTime> <arrivingCity>ORD</arrivingCity> <crewMembers> <item> <employeeId>020040</employeeId> <isDepositor>Y</isDepositor> <isTransmitter>N</isTransmitter> </item> <item> <employeeId>09000</employeeId> <isDepositor>N</isDepositor> <isTransmitter>Y</isTransmitter> </item> </crewMembers> </item> <item> <arrivalDateTime>2010-11-08T20:29:00.000Z</arrivalDateTime> <arrivingCity>JFK</arrivingCity> <crewMembers> <item> <employeeId>0538</employeeId> <isDepositor>Y</isDepositor> <isTransmitter>N</isTransmitter> </item> <item> <employeeId>097790</employeeId> <isDepositor>N</isDepositor> <isTransmitter>Y</isTransmitter> </item> </crewMembers> </item> </flights> </flightInformation> </supplyCrew>\[/code\]This code gets only the first item " and " and then generate a exception 'System.NullReferenceException'.\[code\]using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Linq;using System.Xml.Linq;using System.Text;using System.Xml;using System.Data.SqlClient;using System.Data;using System.IO;namespace ConsoleApplication1{class Program{ static void Main(string[] args) { XElement doc = XElement.Load("U:/Crew.xml"); XNamespace e = "http://schemas.xmlsoap.org/soap/envelope/"; XNamespace s = "http://site.ddf.com"; var yourList2 = doc.Descendants(e + "Body") .Descendants(s + "supplyCrew") .Descendants(s + "flightInformation") .Descendants(s + "flights") .Descendants(s + "item") .Select( x => new { //Anonymous Type arrivalDateTime = x.Element(s + "arrivalDateTime").Value, arrivingCity = x.Element(s + "arrivingCity").Value } ); foreach (var item in yourList2) { Console.Write(item.arrivalDateTime + '\n'); Console.Write(item.arrivingCity + '\n'); } Console.Read(); }}}\[/code\]The idea is to get item.arrivalDateTime and item.arrivingCity and its crewMembers.item.employeeId, isDepositor, isTransmitter for all the rows. However I do not know how to get the childnodes using linq...Eg:
2010-11-08T22:48:00.000Z; ORD; 020040; Y; N2010-11-08T22:48:00.000Z; ORD; 09000; N; Y2010-11-08T20:29:00.000Z; JFK; 0538; Y; N2010-11-08T20:29:00.000Z; JFK; 097790; N; Y
 
Back
Top