我已经完成了研究,并且不明白为什么我的Linq查询不起作用。我试图从这个XML返回一个“Grant”元素列表:
<?xml version="1.0" encoding="utf-8"?>
<root>
<ArrayOfMapItem>
<MapItem>
<Id xmlns="Private.CO.path.type">7D2B99FA-0000-0000-0000-000000000000</Id>
<Map xmlns="Private.CO.path.type">6557A558-7ED9-4199-8585-6FD8610E4119</Map>
<ServiceCollectionId xmlns="Private.CO.path.type">69DB37D4-8A29-4C4E-8671-6C054B9F62E5</ServiceCollectionId>
<ServiceCollectionName xmlns="Private.CO.path.type">S-AVCL-CC-01-DF8_SC</ServiceCollectionName>
<Services xmlns="Private.CO.path.type">S-AVCL-CC-01-DF8</Services>
<Position xmlns="Private.CO.path.type">1350</Position>
</MapItem>
<MapItem>
<Id xmlns="Private.CO.path.type">7D2B99FC-0000-0000-0000-000000000000</Id>
<Map xmlns="Private.CO.path.type">80703716-4BCE-4465-902E-6B92A9BD41AC</Map>
<ServiceCollectionId xmlns="Private.CO.path.type">A8266306-9384-4CAA-9746-DD9279F9A27E</ServiceCollectionId>
<ServiceCollectionName xmlns="Private.CO.path.type">S-AVCL-CC-02-DF8_SC</ServiceCollectionName>
<Services xmlns="Private.CO.path.type">S-AVCL-CC-02-DF8</Services>
<Position xmlns="Private.CO.path.type">1351</Position>
</MapItem>
</ArrayOfMapItem>
<GrantArrays>
<GrantArray>
<Grant ResourceId="6557a558-7ed9-4199-8585-6fd8610e4119" PrincipalExternalId="royce" PrincipalType="Group" ResType="Package" Right="Access">
<Conditions xmlns="http://www.here.com/location/bss">
<TimeExpiration Start="2014-05-01T07:00:00" End="9999-12-31T23:59:59.997" Type="TimeExpiration" Expiration="9999-12-31T23:59:59" />
</Conditions>
</Grant>
<Grant ResourceId="6557a558-7ed9-4199-8585-6fd8610e4119" PrincipalExternalId="peter" PrincipalType="Group" ResType="Package" Right="Access">
<Conditions xmlns="http://www.here.com/location/bss">
<TimeExpiration Start="2014-04-04T19:18:04.963" End="9999-12-31T23:59:59.997" Type="TimeExpiration" Expiration="9999-12-31T23:59:59" />
</Conditions>
</Grant>
</GrantArray>
<GrantArray>
<Grant ResourceId="80703716-4bce-4465-902e-6b92a9bd41ac" PrincipalExternalId="royce" PrincipalType="Group" ResType="Package" Right="Access">
<Conditions xmlns="http://www.here.com/location/bss">
<TimeExpiration Start="2014-05-01T07:00:00" End="9999-12-31T23:59:59.997" Type="TimeExpiration" Expiration="9999-12-31T23:59:59" />
</Conditions>
</Grant>
<Grant ResourceId="80703716-4bce-4465-902e-6b92a9bd41ac" PrincipalExternalId="peter" PrincipalType="Group" ResType="Package" Right="Access">
<Conditions xmlns="http://www.here.com/location/bss">
<TimeExpiration Start="2014-04-04T19:18:05.2" End="9999-12-31T23:59:59.997" Type="TimeExpiration" Expiration="9999-12-31T23:59:59" />
</Conditions>
</Grant>
</GrantArray>
</GrantArrays>
</root>
我在此方法中应用Linq查询,将方法传递给已知存在的ResourceId的属性值:
private static void CheckGrantChanges(string approvemapid) //, string checkmapid)
{
// approvemapid = "6557a558-7ed9-4199-8585-6fd8610e4119". value confirmed several times
var docApproved = XDocument.Load(@"C:\Tools\WorkOnChannelChecker\Daily11ApprovedMapping.xml");
var approveGrants = docApproved.Descendants("GrantArray");
var queryApproved = from a in approveGrants.Elements("Grant")
where a.Attribute("ResourceId").Value == approvemapid
select a;
}
我希望在“queryApproved”中返回两个Grant元素,但它会一直空着。请告诉我这是什么我在这个过程中误解了? 提前谢谢你的帮助!
答案 0 :(得分:1)
感谢Florian。问题是字符串比较。将查询更改为: ...
var queryApproved = from a in approveGrants.Elements("Grant")
where a.Attribute("ResourceId").Value.Equals(approvemapid,StringComparison.OrdinalIgnoreCase)
select a;
... 解决了这个问题。