简体   繁体   English

如何使用C#或VB从XML获取价值?

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

The Below xml format data is part of the SAML response of assertion XML, i dont konw how to pick the NorBefore and NotOnOrAfter from condtions node and AttributeValue from AttributeStatement node, so please help me to get these values from these xml. 下面的xml格式数据是断言XML的SAML响应的一部分,我不知道如何从条件节点中选择NorBefore和NotOnOrAfter,从AttributeStatement节点中选择AttributeValue,所以请帮助我从这些xml中获取这些值。 I am using visual studio 2005 and i need a code in C# or vb. 我正在使用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>

Thanks 谢谢

In VB.NET, it's quite simple. 在VB.NET中,这非常简单。 First, you'll have to define the namespace in the declaration section of your module: 首先,您必须在模块的声明部分中定义名称空间:

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

Replace SomeNamespace with the namespace declared as ns2 in the header of your XML file. 用XML文件头中声明为ns2的名称空间替换SomeNamespace。

Then load the XML into an XDocument and read the value using VB's LINQ-to-XML syntax: 然后将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