私はこのようなxmlを持っています:
<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>
このコードは、最初の項目 " および " のみを取得し、例外 'System.NullReferenceException' を生成します。
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 flights = doc.Elements(e + "Body")
.Elements(s + "supplyCrew")
.Elements(s + "flightInformation")
.Elements(s + "flights")
.Elements(s + "item")
.Select(
flight_item => new
{
//Anonymous Type
arrivalDateTime = flight_item.Element(s + "arrivalDateTime").Value,
arrivingCity = flight_item.Element(s + "arrivingCity").Value,
crewmember = flight_item.Elements(s + "crewMembers").Elements(s + "item"),
}
);
int index = 1;
foreach (var flight_item in flights)
{
Console.Write('\n'+"New flight item:"+'\n');
Console.Write('\t'+flight_item.arrivalDateTime + '\n');
Console.Write('\t'+flight_item.arrivingCity + '\n');
foreach (var item in flight_item.crewmember)
{
var employeeId = item;//crewmember.Elements(s + "item").Elements("employeeId");
Console.Write("\t "+employeeId);
Console.Write('\n');
//index++;
}
}
アイデアは、すべての行の item.arrivalDateTime と item.arrivingCity およびその cargoMembers.item.employeeId を取得することです。ただし、linq を使用して子ノードを取得する方法がわかりません。employeeId = x.Element(s + "employeeId").Value....
結果は次のようになります。
2010-11-08T22:48:00.000Z; ORD; 020040
2010-11-08T22:48:00.000Z; ORD; 09000
2010-11-08T20:29:00.000Z; JFK; 0538
2010-11-08T20:29:00.000Z; JFK; 097790