我想从XML文档中获得一个使用linq的XElement。
我的linq查询是:
webDetails.Descendants("WebApplications")
.Descendants("WebApplication")
.Where(x => x.Attribute("Uri").Value.Equals(selectedItem.Value))
.FirstOrDefault();该文件包含以下xml节点:
<?xml version="1.0" encoding="UTF-8"?>
<WebApplications>
<WebApplication Name="SharePoint - 80" Uri="http://xxxx/">
<SiteCollections>
<SiteCollection>
<RootWeb Title="Root" Url="/">
<Webs />
</RootWeb>
</SiteCollection>
<SiteCollection>
<RootWeb Title="My Site Host" Url="/my/HostSite">
<Webs />
</RootWeb>
</SiteCollection>
</SiteCollections>
</WebApplication>
<WebApplication Name="SharePoint - 9999" Uri="http://xxx/">
<SiteCollections>
<SiteCollection>
<RootWeb Title="Ritesh Goswami" Url="/my/rami">
<Webs />
</RootWeb>
</SiteCollection>
<SiteCollection>
<RootWeb Title="Riyaz Kalva" Url="/my/rialva">
<Webs />
</RootWeb>
</SiteCollection>
</SiteCollections>
</WebApplication>
</WebApplications>如果我执行如下查询:
webDetails.Descendants("WebApplications").Descendants("WebApplication").First()我正在获取第一个元素,但是当我使用属性值进行过滤时,我得到了上面提到的错误。如何使用属性值过滤xmlelement?
发布于 2014-02-13 17:18:17
尝试使用以下命令:
webDetails.Descendants("WebApplications").Descendants("WebApplication").Where(x => string.Equals(x.Attribute("Uri").Value, selectedItem.Value, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();发布于 2014-02-13 17:28:26
试试这个。
string s = selectedItem.Value;
webDetails.Descendants("WebApplications").Descendants("WebApplication").Where(x => x.Attribute("Uri").Value.Equals(s)).FirstOrDefault();https://stackoverflow.com/questions/21749527
复制相似问题