首页 > 解决方案 > 从xml c#获取属性值

问题描述

<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-parsingxmldocument

解决方案


获取价值

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

推荐阅读