首页 新闻 会员 周边 捐助

Linq to xml .net3.5 查询失败

0
悬赏园豆:20 [已解决问题] 解决于 2010-09-14 17:46

xml:

<?xml version="1.0" encoding="utf-8" ?><EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata" 

 xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"

xmlns:ds="http://www.w3.org/2000/09/xmldsig#"

entityID="http://10.162.1.220:8082/bspcore">  

<IDPSSODescriptor WantAuthnRequestsSigned="true"

protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">    <SingleSignOnService isDefault="true" index="0"  

Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"  

Location="http://10.162.1.220:8082/bspcore/SAML2/POST/SSO" />    <SingleSignOnServiceBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"Location="http://10.162.1.220:8082/bspcore/SAML2/REDIRECT/SSO" />    <ArtifactResolutionServiceBinding="urn:oasis:names:tc:SAML:2.0:bindings:SOAP"

Location="http://10.162.1.220:8082/bspcore/SAML2/Artifact" />    <SingleLogoutServiceBinding="urn:oasis:names:tc:SAML:2.0:bindings:SOAP"

Location="http://10.162.1.220:8082/bspcore/SAML2/SLO/SOAP" />    <SingleLogoutServiceBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"Location="http://10.162.1.220:8082/bspcore/SAML2/SLO/REDIRECT"

ResponseLocation="http://10.162.1.220:8082/bspcore/SAML2/SLO/Response" />    <NameIDFormat>      urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName    </NameIDFormat>    <NameIDFormat>      urn:oasis:names:tc:SAML:2.0:nameid-format:persistent    </NameIDFormat>    <NameIDFormat>      urn:oasis:names:tc:SAML:2.0:nameid-format:transient    </NameIDFormat>  </IDPSSODescriptor></EntityDescriptor>

 

代码:

 

XElement idpdoc = XElement.Load(new StreamReader("E:\\IDP.xml"));            

var test = idpdoc.Descendants("NameIDFormat");
var NameIdFormats = (from ni in idpdoc.Descendants() where ni.Name=="NameIDFormat" select ni.Value).ToList<String>();

 

结果:

test.Count()=0;

NameIdFormats .Count()=0;

镜涛的主页 镜涛 | 小虾三级 | 园豆:575
提问于:2010-08-25 15:47
< >
分享
最佳答案
0

你的XML好像格式不太正确,建议你格式化下,我格式化后的XML:

<?xml version="1.0" encoding="utf-8" ?>
<EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata"
xmlns:saml
="urn:oasis:names:tc:SAML:2.0:assertion"
xmlns:ds
="http://www.w3.org/2000/09/xmldsig#"
entityID
="http://10.162.1.220:8082/bspcore">

<IDPSSODescriptor WantAuthnRequestsSigned="true"
protocolSupportEnumeration
="urn:oasis:names:tc:SAML:2.0:protocol">
<SingleSignOnService isDefault="true" index="0"
Binding
="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
Location
="http://10.162.1.220:8082/bspcore/SAML2/POST/SSO" />
<SingleSignOnServiceBinding xmlns="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
Location
="http://10.162.1.220:8082/bspcore/SAML2/REDIRECT/SSO" />
<ArtifactResolutionServiceBinding Location="http://10.162.1.220:8082/bspcore/SAML2/Artifact" />
<SingleLogoutServiceBinding Location="http://10.162.1.220:8082/bspcore/SAML2/SLO/SOAP" />
<SingleLogoutServiceBinding xmlns="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
Location
="http://10.162.1.220:8082/bspcore/SAML2/SLO/REDIRECT"
ResponseLocation
="http://10.162.1.220:8082/bspcore/SAML2/SLO/Response" />
<NameIDFormat> urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName </NameIDFormat>
<NameIDFormat> urn:oasis:names:tc:SAML:2.0:nameid-format:persistent </NameIDFormat>
<NameIDFormat> urn:oasis:names:tc:SAML:2.0:nameid-format:transient </NameIDFormat>
</IDPSSODescriptor>
</EntityDescriptor>

至于为什么会出现这样的问题,你可以将下面代码执行并打印就可以发现了:

var NameIdFormats = from ni in idpdoc.Descendants()
select ni.Name;

打印出来的代码是,包含了具体的命名空间:

{urn:oasis:names:tc:SAML:2.0:metadata}IDPSSODescriptor
{urn:oasis:names:tc:SAML:2.0:metadata}SingleSignOnService
{urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect}SingleSignOnServiceBinding
{urn:oasis:names:tc:SAML:2.0:metadata}ArtifactResolutionServiceBinding
{urn:oasis:names:tc:SAML:2.0:metadata}SingleLogoutServiceBinding
{urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect}SingleLogoutServiceBinding
{urn:oasis:names:tc:SAML:2.0:metadata}NameIDFormat
{urn:oasis:names:tc:SAML:2.0:metadata}NameIDFormat
{urn:oasis:names:tc:SAML:2.0:metadata}NameIDFormat

所以你传NameIDFormat是匹配不到的。

 你可以试下这个方法:

 

var NameIdFormats = from ni in idpdoc.Elements()
let ni2
= ni.Elements()
from ni3
in ni2
where ni3.Name == ni3.GetDefaultNamespace() + "NameIDFormat"
select ni3.Value;

 

 

收获园豆:20
kyo-yo | 大侠五级 |园豆:5587 | 2010-08-25 16:17
//Querying XML containing namespaces with LINQ to XML //Fully expanded query using an XNamespace and XName XElement rss = XElement.Load("http://iqueryable.com/rss.aspx"); XNamespace dc = "http://purl.org/dc/elements/1.1/"; XNamespace slash = "http://purl.org/rss/1.0/modules/slash/"; XNamespace wfw = "http://wellformedweb.org/CommentAPI/"; IEnumerable<XElement> comments = rss.Descendants(slash + "comments"); foreach(XElement comment in comments) { Console.WriteLine((int)comment); } //Query using only a local name IEnumerable<XElement> titles = rss.Descendants("title"); foreach(XElement title in titles) { Console.WriteLine((string)title); } <?xml-stylesheet href=http://iqueryable.com/friendly-rss.xsl type="text/xsl" media="screen"?> <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"> <channel> <title>Steve Eichert</title> <link>http://iqueryable.com/</link> <generator>ActiveType CMS v0.1</generator> <dc:language>en-US</dc:language> <description /> <item> <dc:creator>Steve Eichert</dc:creator> <title>Parsing WordML using LINQ to XML</title> <link>http://iqueryable.com/LINQ/ParsingWordMLusingLINQ to XML</link> <pubDate>Wed, 02 Aug 2006 15:52:44 GMT</pubDate> <guid>http://iqueryable.com/LINQ/ParsingWordMLusingLINQ to XML</guid> <comments> http://iqueryable.com/LINQ/ParsingWordMLusingLINQ to XML#comments </comments> <wfw:commentRss> http://iqueryable.com/LINQ/ParsingWordMLusingLINQ to XML/commentRss.aspx </wfw:commentRss> <slash:comments>1</slash:comments> <description>Foo…</description> </item> </channel>
镜涛 | 园豆:575 (小虾三级) | 2010-08-25 16:31
或者直接说明下不指定命名空间的查找方式,带命名空间太麻烦
镜涛 | 园豆:575 (小虾三级) | 2010-08-25 16:35
@镜涛:我用了个不是办法的办法,你看看
kyo-yo | 园豆:5587 (大侠五级) | 2010-08-25 16:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册