【问题标题】:Get attribute value from xml c#从xml c#获取属性值
【发布时间】:2021-11-17 09:25:03
【问题描述】:
<samlp:Response ID="omlaceop"
                IssueInstant="2021-09-22T01:52:44.494Z"
                Version="2.0"
                Destination="https://example.com"
                InResponseTo="_4b625517-b486-4b8a-b321-33e3f81231da"
                xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
                xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
    <saml:Issuer>myself</saml:Issuer>
    <samlp:Status>
        <samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
    </samlp:Status>
    <saml:Assertion ID="baaoepbe"
                    IssueInstant="2021-09-22T01:52:44.494Z"
                    Version="2"
                    xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
        <saml:Issuer>me</saml:Issuer>
        <saml:Subject>
            <saml:NameID Format="urn:oasis:names:tc:2.0:nameidformat:persistent">46</saml:NameID>
            <saml:SubjectConfirmation>
                <saml:SubjectConfirmationData Recipient="https://example.com"
                                                NotOnOrAfter="2021-09-22T02:02:44.494Z"
                                                InResponseTo="_4b625517-b486-4b8a-b321-33e3f81231da" />
            </saml:SubjectConfirmation>
        </saml:Subject>
    </saml:Assertion>
</samlp:Response>

我尝试了一些与此错误相关的帖子,但没有运气,并且我尝试使用 xmlDocument 但无法获取 InResponseTo 的值。非常感谢任何帮助。

方法1(没用)

var xdoc = XDocument.Load("samlresponse.xml");

            var items = from i in xdoc.Descendants("saml:SubjectConfirmationData")
                select new
                {
                    Recipient = (string)i.Attribute("InResponseTo")
                };

方法2(没用)

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("samlresponse.xml");

var SubjectConfirmationnodes = xmlDocument.SelectNodes(@"//saml:Subject/saml:SubjectConfirmation");

【问题讨论】:

    标签: c# xml-parsing xmldocument


    【解决方案1】:

    获取价值

    XmlNodeList nodeList = xmlDocument.GetElementsByTagName("saml:SubjectConfirmationData");
                var InResponseTo = nodeList[0].Attributes[2].Value;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-16
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      相关资源
      最近更新 更多