Search code examples
c#.net-corerestsharp

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


I have this code which returns a RestSharp objects for my request and Response:

        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;

I then have a method with the following code to get the element I need:

    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;
    }

I can see in the debugger that response.Content does indeed have XML. I specified the request object format its data via XmL. But when I get the the line to get the specific value, I see a null value. I am not certain where to go from here.

I believe something here is not correct with this line code:

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

Here is the upper section of the response.Content xml which contains the element and value I am after:

"<?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>

Solution

  • Make sure you have valid xml data as the data you had provided is missing the closing transaction and nm_response tags which might cause a parsing issue. When it comes to selecting the node you are looking for, you can set the selection string to the following,

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

    This should select the specific node you are looking for and allow you to get the inner text by using the following.

    String cc_Number = node.InnerText;