Search code examples
c#asp.netxmlstringbuilder

Trying to get XML value from Stringbuilder String - C# (Moneris Data Preload)


I am writing a code to pull a specific value from Stringbuilder. The idea is, I am submitting a form to Moneris - A payment gateway (I am using a sample key and id so there is no confidential information is mentioned in here) to receive a dynamically generated key from Moneris.

Please see my code below:

StringBuilder sb = new StringBuilder();
    sb.Append("<html>");
    sb.AppendFormat(@"<body onload='document.forms[""form""].submit()'>");
    sb.AppendFormat("<form name='form' action='{0}' method='post'>", "https://esqa.moneris.com/HPPDP/index.php");
    sb.AppendFormat("<input type='hidden' name='ps_store_id' value='{0}'>", "R6SXStore3");
    sb.AppendFormat("<input type='hidden' name='hpp_key' value='{0}'>", "hpZPXLXZNBLF");
    sb.AppendFormat("<input type='hidden' name='charge_total' value='{0}'>", "2.00");
    sb.AppendFormat("<input type='hidden' name='hpp_preload' value='{0}'>", "");
    sb.AppendFormat("<input type='hidden' name='order_id' value='{0}'>", "");
    sb.Append("</form>");
    sb.Append("</body>");
    sb.Append("</html>");

Response.Write(sb.ToString()); // This is submitting the above form to the moneris (third party payment website and throwing values in a kind of XML format).

Please see the screenshot https://snag.gy/OHbk6y.jpg of what response i am getting from Moneris.

I am interested in pulling value from "ticket" node which i have highlighted in the screenshot above.

This is a code i am writing to pull value from "ticket" node.

XmlDocument xmlDoc = new XmlDocument();
//  string myXML = @"<!--?xml version='1.0' standalone='yes'?--><html><head></head><body><response><hpp_id>R6SXStore3</hpp_id><ticket>hpp1529956212E2mefmVB93Yu2taJy</ticket><order_id></order_id><response_code>1</response_code></response></body></html>";
string myXML = sb.ToString();
xmlDoc.LoadXml(myXML);
XmlNodeList parentNode = xmlDoc.GetElementsByTagName("response");
string xticket;
string xhpp_id;
foreach (XmlNode childrenNode in parentNode)
    {
        HttpContext.Current.Response.Write(childrenNode.SelectSingleNode("ticket").InnerText);
    }
Response.End();

Error:

When i run above mentioned code, i see this error: Please see the screenshot https://snag.gy/PJMKqL.jpg

However, when i un-comment my code where i am passing a hardcoded value in the variable "myXML" then i get my result perfectly. The hardcoded value is what i am pulling from browser's source code when i get a response from Moneris.

Can you please help me to solve this issue.


Solution

  • You can use a WebClient to do that. I've also added a few lines to work over SSL, if you need it.

    public void test()
        {
            var postData = "ps_store_id=R6SXStore3";
            postData += "&hpp_key=hpZPXLXZNBLF";
            postData += "&charge_total=2.00";
            postData += "&hpp_preload=";
            postData += "&order_id=";
    
            var encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(postData);
    
            var myRequest = (HttpWebRequest)WebRequest.Create("https://esqa.moneris.com/HPPDP/index.php");
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;
    
            //This code is to work using SSL
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
    
            //Post the content
            var newStream = myRequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
    
            //Read the response
            var response = myRequest.GetResponse();
            var responseStream = response.GetResponseStream();
            var responseReader = new StreamReader(responseStream);
            var result = responseReader.ReadToEnd();
    
            responseReader.Close();
            response.Close();
    
            //Your original code
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(result); //Load the response into the XML
            XmlNodeList parentNode = xmlDoc.GetElementsByTagName("response");
            string xticket;
            string xhpp_id;
            foreach (XmlNode childrenNode in parentNode)
            {
                xticket = childrenNode.SelectSingleNode("ticket").InnerText;
                xhpp_id = childrenNode.SelectSingleNode("hpp_id").InnerText;
            }
        }
    
        public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }