这是我得到的示例XML:
<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>
这是代码:
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();
结果是null,为什么会这样?
答案 0 :(得分:1)
loadedData.Descendants("ArrayOfService")
应为loadedData.Root
有关详细信息,请参阅XDocument.Root (MSDN)。
答案 1 :(得分:0)
我找不到问题,但我只是将xml的内容复制到另一个文件中,然后就可以了。