Search code examples
c#httpclientrestsharp

How to verify XML data using HttpClient in C#


I am new to C# and I'm having difficulty in validating some data in API response.

I need to navigate to the XML path to assert the expected value with returned value.

Here is my code:

[Test]
public void ValidatePostRequest()
{
    XmlDocument doc = new XmlDocument();
    doc.Load("X:\\xmlFile.xml");
    XmlNode node = doc.DocumentElement.SelectSingleNode("/PARENTNODE");
    string requestNode = node.OuterXml;
    using (HttpClient httpClient = new HttpClient())
    {
        HttpContent httpContent = new StringContent(requestNode, Encoding.UTF8, contentType);
        Task<HttpResponseMessage> postResponse
            = httpClient.PostAsync(postURL, httpContent);
        HttpStatusCode statusCode = postResponse.Result.StatusCode;
        HttpContent responseContent = postResponse.Result.Content;
        string responseData = responseContent.ReadAsStringAsync().Result;

        Task<String> resData = responseContent.ReadAsStringAsync();
        string data = resData.Result;

        Console.WriteLine("Getting Status Code: " + (int)statusCode);
        Console.WriteLine("Returning body data:  " + data.ToString());

        //Need to verify the value returned in response for StatusMsgsCode 
        XmlNode msgStatusXMLNode = doc.DocumentElement.SelectSingleNode("/Trans/Output/ACORD/HomeLineBusiness/Construction/StatusMessage/StatusMsgCode");

        //XmlNodeList msgStatusCd = doc.DocumentElement.GetElementsByTagName("/Trans/Output/ACORD/HomeLineBusiness/Construction/StatusMessage/StatusMsgCode");

        if (msgStatusXMLNode != null)
        {
            Console.WriteLine(msgStatusXMLNode.InnerText);
        }
        else
        {
            Console.WriteLine("No value returned");
        }
    }
}

Solution

  • Here is the solution which worked for me. For verification of xml data after extracting the node details.

    [Test]
        public void ValidatePostRequest()
        {
       
        //Load the payload for POST request from XML file
            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\XMLFile.xml");
            XmlNode node = doc.DocumentElement.SelectSingleNode("/ParentNode");
            string requestNode = node.OuterXml;
            using (HttpClient httpClient = new HttpClient())
            {
                HttpContent httpContent = new StringContent(requestNode, Encoding.UTF8, contentType);
                Task<HttpResponseMessage> postResponse
                     = httpClient.PostAsync(postURL, httpContent);
                     //Gets the statusCode for POST response
                HttpStatusCode statusCode = postResponse.Result.StatusCode;
                HttpContent responseContent = postResponse.Result.Content;
                
            );
            Task<String> resData = responseContent.ReadAsStringAsync();
                
                //Response body is saved in string format
                string data = resData.Result;
    
              // Conversion of String to XML  Node
                XmlDocument document = new XmlDocument();
                document.LoadXml(data);
                XmlNode newNode = document.DocumentElement;
                // Navigate to the node path for data extraction
            XmlNode msgStatusNode = newNode.SelectSingleNode("/Parent/ChildNode1/Node1");
              
                  if (msgStatusNode != null)
                {
                    Console.WriteLine(msgStatusNode.InnerText);
                }
                else
                {
                    Console.WriteLine("Returning null");
                }
    
            }
        }