Je suis nouveau sur C# et j'ai des difficultés à valider certaines données dans la réponse de l'API.

J'ai besoin de naviguer vers le chemin XML pour affirmer la valeur attendue avec la valeur renvoyée.

Voici mon 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");
        }
    }
}
0
Panee S 15 oct. 2020 à 07:24

1 réponse

Meilleure réponse

Voici la solution qui a fonctionné pour moi. Pour la vérification des données XML après l'extraction des détails du nœud.

[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");
            }

        }
    }
0
Panee S 15 oct. 2020 à 17:56