I am using HttpWebRequest and HttpWebResponse to POST data to a web service and receive the resulting XML response. I have never worked with XML before (only been coding for a few months) so I have been peicing everything together from research and trial/error. At this point I have the code up to the point of sending the data, receiving the response and putting the response into an Xdocument. Now I need to grab bits of it to put into variables so I can make some decisions of the next step, based on the values in the XML response. This is what a common response looks like:
For example, I need to get the value of 'Matches' and do stuff depending on the value.
This code works and is currently storing the XML in the Xdocument called resultLoad. I want to be able to search the document for the <Matches> value, then put that value in a variable, and so forth. The research I have done says to put the whole response in an XML doc, save it somewhere, then use LINQ to XML to query it, but I really dont want to do all that. i just want to do it all in memory. Here is the code to this point.
<?xml version="1.0" encoding="UTF-8"?> <HistoryResponse> <ResponseType>resultsList</ResponseType> <Matches>0</Matches> <SessionID>75803234r23df3de</SessionID> <RecStart>0</RecStart> <ClientCode></ClientCode> <Results></Results> </HistoryResponse>
For example, I need to get the value of 'Matches' and do stuff depending on the value.
This code works and is currently storing the XML in the Xdocument called resultLoad. I want to be able to search the document for the <Matches> value, then put that value in a variable, and so forth. The research I have done says to put the whole response in an XML doc, save it somewhere, then use LINQ to XML to query it, but I really dont want to do all that. i just want to do it all in memory. Here is the code to this point.
public ActionResult SubmitToCris(NewApplicantViewModel model) { // Setup my variables string First = model.PersonModel.FirstName; string Last = model.PersonModel.LastName; string dob = model.PersonModel.DateofBirth.ToString("yyyy-MM-dd"); string historyURL = "https://www.nunya.com/XMLServer/XMLServer.cgi"; //Build my data to be sent in key/value pair string string postData = "MaxRecords=0&UserID=skapi&Password=sk12024&Version=2.0&RequestType=searchName&FirstName=" + First + "&LastName=" + Last + "&DOB=" + dob; //Create the web request, populate the header info HttpWebRequest request = (HttpWebRequest)WebRequest.Create(crisURL); request.Method = "POST"; request.ContentLength = postData.Length; request.ContentType = "application/x-www-form-urlencoded"; // Send it using (Stream writeStream = request.GetRequestStream()) { byte[] byteArray = Encoding.UTF8.GetBytes(postData); writeStream.Write(byteArray, 0, byteArray.Length); writeStream.Close(); } // Receive the response and do stuff with it. string result = string.Empty; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using( Stream responseStream = response.GetResponseStream() ) { using( StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8)) { result = readStream.ReadToEnd(); } } } XDocument resultLoad = Xdocument.Parse(result); ViewBag.XmlResponse = resultLoad.ToString(); return View(); }