簡體   English   中英

從 Xml 響應中獲取特定元素值 (RestSharp/C#)

[英]Obtain Specific Element Value From Xml Response (RestSharp/C#)

我有這段代碼為我的請求和響應返回一個 RestSharp 對象:

        var request = new RestRequest(Method.POST);
        request.AddParameter("Content-Type", "application/xml", ParameterType.HttpHeader);
        request.AddParameter("security_key", apiKey, ParameterType.QueryString);
        request.AddParameter("transaction_id", transactionDetailsResource.TransactionId, ParameterType.QueryString);
        request.RequestFormat = DataFormat.Xml;
        response = client.Execute(request) as RestResponse;

然后我有一個帶有以下代碼的方法來獲取我需要的元素:

    public static TransactionDetailsDto convertContent(RestResponse response){
        TransactionDetailsDto tranDto = new TransactionDetailsDto();

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(response.Content);
        XmlElement root = doc.DocumentElement;
        XmlNode node = root.SelectSingleNode("/cc_number");
        tranDto.maskedCCNumber = node.Value;
        tranDto.response = response.Content;
        return tranDto;
    }

我可以在調試器中看到response.Content確實有 XML。 我指定了請求 object 通過 XmL 格式化其數據。 但是當我得到獲取特定值的行時,我看到了一個 null 值。 我不確定 go 從這里到哪里。

我相信此行代碼不正確:

XmlNode node = root.SelectSingleNode("/cc_number");

這是response.Content的上半部分。內容 xml 包含我所追求的元素和值:

"<?xml version=\"1.0\" encoding=\"UTF-8\"?><nm_response><transaction><transaction_id>5432920844</transaction_id><partial_payment_id></partial_payment_id><partial_payment_balance></partial_payment_balance><platform_id>54e69fa3-36af-ea11-8ada-0050569419e2</platform_id><transaction_type>cc</transaction_type><condition>complete</condition><order_id>54e69fa3-36af-ea11-8ada-0050569419e2</order_id><authorization_code>857882</authorization_code><ponumber></ponumber><order_description></order_description><first_name></first_name><last_name>07675018324$02500$</last_name><address_1></address_1><address_2></address_2><company></company><city></city><state></state><postal_code>30041</postal_code><country></country><email></email><phone></phone><fax></fax><cell_phone></cell_phone><customertaxid></customertaxid><customerid></customerid><website></website><shipping_first_name></shipping_first_name><shipping_last_name></shipping_last_name><shipping_address_1></shipping_address_1><shipping_address_2></shipping_address_2><shipping_company></shipping_company><shipping_city></shipping_city><shipping_state></shipping_state><shipping_postal_code></shipping_postal_code><shipping_country></shipping_country><shipping_email></shipping_email><shipping_carrier></shipping_carrier><tracking_number></tracking_number><shipping_date></shipping_date><shipping>0.00</shipping><shipping_phone></shipping_phone><cc_number>4xxxxxxxxxxx7254</cc_number>

確保您擁有有效的 xml 數據,因為您提供的數據缺少可能導致解析問題的關閉事務和 nm_response 標記。 在選擇您要查找的節點時,您可以將選擇字符串設置為以下內容,

XmlNode node = root.SelectSingleNode("/nm_response/transaction/cc_number")

這應該 select 您正在尋找的特定節點,並允許您使用以下內容獲取內部文本。

String cc_Number = node.InnerText;

暫無
暫無

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

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