首页 > 解决方案 > C# 和 XML 选择器

问题描述

不幸的是,我对 C# 一无所知,我只是尝试翻译下面的一段代码。

var body = Document.SelectSingleNode("//soap:Body/*[1]", Manager);

这里选择了哪个元素? <ds:KeyInfo>吗?

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Id="ED-1B758D26C51BFCD86614340101135852" Type="http://www.w3.org/2001/04/xmlenc#Content">
            <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc"/>
            <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
                <wsse:SecurityTokenReference xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsse11="http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd" wsse11:TokenType="http://docs.oasis-open.org/wss/oasis-wss-soap-message-security-1.1#EncryptedKey">
                    <wsse:Reference URI="#EK-1B758D26C51BFCD86614340101135741"/>
                </wsse:SecurityTokenReference>
            </ds:KeyInfo>
            <xenc:CipherData>
                <xenc:CipherValue></xenc:CipherValue>
            </xenc:CipherData>
        </xenc:EncryptedData>
    </soap:Body>
</soap:Envelope>

标签: c#xmlsoap

解决方案


我建议您看一下XPath Syntax,它应该对您有很大帮助。

让我用这个表达式来解释你现在正在使用什么元素//soap:Body/*[1]:该//表达式意味着您将采用与选择匹配的节点,在这种情况下,soap:Body无论它在哪里。该/表达式告诉您从该节点获取一个子节点。*通配符匹配任何元素节点,并告诉[1]它获取第一个子节点。

在这一切之后,你得到的当前节点是xenc:EncryptedData.


推荐阅读