Showing results for 
Search instead for 
Did you mean: 

GetTransactionDetails XML web service call sample

I have reviewed the XML sample code, and wanted to know if there was any C# sample code to actually make the call (HTTP POST).


This is the XML that I want to send:


<?xml version="1.0" encoding="utf-8"?>
<getTransactionDetailsRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">


So in other words, how do I package and send this code in my C# web application.


You mean how to do xml call in c#?

CIM - C#

have example on how to do xml call.


It seems to show good examples of how to process the XML response, but not seeing any sample code on how to take a piece of existing XML code, and send it as a request. For example, PostRequest method takes ApiRequest of type Object as one of it's parameters, and then serializes it . I'm new to C# so I'm not sure how serializer works, and whether it really matters in this situation.


I'm not sure how to process a XDocument using PostRequest or similar.



Thanks much. Just to make sure, would the following be correct:


public const String API_URL = "";

public static void Main()
    XNamespace ns = "AnetApi/xml/v1/schema/AnetApiSchema.xsd";

            XDocument doc = new XDocument(
                    new XElement(ns + "getTransactionDetailsRequest",
                        new XAttribute("xmlns", "AnetApi/xml/v1/schema/AnetApiSchema.xsd"),
                    new XElement(ns + "merchantAuthentication",
                        new XElement(ns + "name", TEST_NAME),
                        new XElement(ns + "transactionKey", TEST_TRANS_KEY)),
                    new XElement(ns + "transId", TEST_TRANS_ID))

    System.Xml.XmlDocument response_xml = null;
    PostRequest(doc.ToString(), out response_xml);

		public static bool PostRequest(object ApiRequest, out XmlDocument xmldoc)
			bool bResult = false;
			XmlSerializer serializer;

			xmldoc = null;

				HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(API_URL);
				webRequest.Method = "POST";
				webRequest.ContentType = "text/xml";
				webRequest.KeepAlive = true;

				// Serialize the request
				serializer = new XmlSerializer(ApiRequest.GetType());
				XmlWriter writer = new XmlTextWriter(webRequest.GetRequestStream(), Encoding.UTF8);
				serializer.Serialize(writer, ApiRequest);

				if (PRINT_POST)
					StreamWriter consoleOutput = new StreamWriter(Console.OpenStandardOutput());
					consoleOutput.AutoFlush = true;
					serializer.Serialize(consoleOutput, ApiRequest);

				// Get the response
				WebResponse webResponse = webRequest.GetResponse();

				// Load the response from the API server into an XmlDocument.
				xmldoc = new XmlDocument();

				if (PRINT_POST)
					XmlWriterSettings settings = new XmlWriterSettings();
					settings.Indent = true;
					settings.Encoding = Encoding.ASCII;
					XmlWriter consoleWriter = XmlWriter.Create(Console.Out, settings);

				bResult = true;
			catch (Exception ex)
				Console.WriteLine(ex.GetType().ToString() + ": " + ex.Message);
				bResult = false;

			return bResult;


Try it and debug it.

Since I'm not familiar with serializer, I modified the PostRequest method in the API to meet my needs. It seems like I am getting the response from Authorize.NET, and can see some XML, but I am having trouble capturing the responseReasonCode node's value (or for that matter -- I can't even get the node as it says it is NULL in the code below).


            XNamespace ns = "AnetApi/xml/v1/schema/AnetApiSchema.xsd";

            XDocument doc = new XDocument(
                    new XElement(ns + "getTransactionDetailsRequest",
                        new XAttribute("xmlns", "AnetApi/xml/v1/schema/AnetApiSchema.xsd"),
                    new XElement(ns + "merchantAuthentication",
                        new XElement(ns + "name", TEST_NAME),
                        new XElement(ns + "transactionKey", TEST_TRANS_KEY)),
                    new XElement(ns + "transId", TEST_TRANS_ID))

            MemoryStream stream = new MemoryStream();
            byte[] postBytes = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(postBytes, 0, postBytes.Length);

            WebClient WC = new WebClient();
            ServicePointManager.ServerCertificateValidationCallback = TrustAllCertificateCallback;
            HttpWebRequest wrequest = (HttpWebRequest)WebRequest.Create(IsLive ? LIVE_URL : TEST_URL);
            wrequest.Timeout = 300000;
            wrequest.Method = "POST";
            wrequest.ContentType = "text/xml";
            wrequest.ContentLength = stream.Length;

            Stream lilriver = wrequest.GetRequestStream();
            lilriver.Write(postBytes, 0, postBytes.Length);

            WebResponse webResponse = wrequest.GetResponse();

            XDocument xdoc = XDocument.Load(XmlReader.Create(webResponse.GetResponseStream()));
            //var node = xdoc.Descendants().Where(n => n.Name == "responseReasonCode").FirstOrDefault();
            var node = xdoc.Descendants("responseReasonCode").FirstOrDefault();

            if (node != null)
                reason = node.Value;


Why not just use the soap/web service? then you don't have to deal with xml at all.