簡體   English   中英

如何使用C#或VB從XML獲取價值?

[英]How to get value from XML using C# or VB?

下面的xml格式數據是斷言XML的SAML響應的一部分,我不知道如何從條件節點中選擇NorBefore和NotOnOrAfter,從AttributeStatement節點中選擇AttributeValue,所以請幫助我從這些xml中獲取這些值。 我正在使用Visual Studio 2005,並且需要C#或VB中的代碼。

<ns2:Conditions NotBefore="2014-01-16T22:10:09Z" NotOnOrAfter="2014-01-16T22:12:09Z">
<ns2:AudienceRestriction>
<ns2:Audience>namecomponent:test.comp.com</ns2:Audience>
</ns2:AudienceRestriction>
</ns2:Conditions>
<ns2:AuthnStatement AuthnInstant="2014-01-16T22:10:39Z" SessionIndex="8cILoZqzVAFS2t8zF65fmT2rIFE=F5zAiA==" SessionNotOnOrAfter="2014-01-16T22:12:09Z">
<ns2:AuthnContext>
<ns2:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password</ns2:AuthnContextClassRef>
</ns2:AuthnContext>
</ns2:AuthnStatement>
<ns2:AttributeStatement>
<ns2:Attribute Name="NameID" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified">
<ns2:AttributeValue>EMP10344</ns2:AttributeValue>
</ns2:Attribute>
</ns2:AttributeStatement>

謝謝

在VB.NET中,這非常簡單。 首先,您必須在模塊的聲明部分中定義名稱空間:

Imports <xmlns:ns2="http://SomeNamespace">

用XML文件頭中聲明為ns2的名稱空間替換SomeNamespace。

然后將XML加載到XDocument中,並使用VB的LINQ-to-XML語法讀取值:

Dim notBefore = xdoc.Root.<ns2:Conditions>.@NotBefore

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM