我有这样的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 及其 crampMembers.item.employeeId。但是我不知道如何使用 linq 获取子节点...如果尝试 employeeId = x.Element(s + "employeeId").Value....
结果应该是这样的:
2010-11-08T22:48:00.000Z; 订单;020040
2010-11-08T22:48:00.000Z; 订单;09000
2010-11-08T20:29:00.000Z; 肯尼迪;0538
2010-11-08T20:29:00.000Z; 肯尼迪;097790