Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

C#/MVC XML Parsing

$
0
0

Hi All,

I have an application which generates a URL based on user requirements and then submits it to a 3rd party for processing, which returns XML back to the client for manipulation and further processing.

I have hit a wall when it comes to extracting information out form the XML stream which i receive back.

The following code shows what I have come up with so far;

            try
            {
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(requestUrl);

                // Get the associated response for the above request.
                using (HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse())
                {
                    myHttpWebResponse.Close();
                }
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    var response = ((HttpWebResponse)e.Response);
                    ViewBag.httpstatuscode = response.StatusCode;
                    ViewBag.errorreceived = response.StatusDescription;

                    try
                    {
                        using (var stream = response.GetResponseStream())
                        {
                            using (var reader = new StreamReader(stream))
                            {
                                string httpresponse = reader.ReadToEnd();

                                XmlDocument xmlDoc = new XmlDocument();
                                xmlDoc.LoadXml(httpresponse);

                                XmlNodeList code = xmlDoc.GetElementsByTagName("Code");
                                XmlNodeList message = xmlDoc.GetElementsByTagName("Message");
                                XmlNodeList requestid = xmlDoc.GetElementsByTagName("RequestID");
                                if(message != null) {
                                    foreach(XmlNode data in message) {
		                                ViewBag.httpmessage = data.InnerText;
                                    }
                                }
                            }
                        }
                    }
                    catch (WebException ex)
                    {
                        // Oh, well, we tried
                    }
                }
            }
            catch (Exception e)
            {
                ViewBag.exceptioncatcher = "The URL used to process your request is incorrect and was rejected by the server, the error received was - " + e.Message;
            }

So looking above, I wish not to use a for loop to read through the xml string as there is only a single set of data per xml node, i.e. <something> only appears once and this is consistant. Also I wish to output the values from 3 nodes in the XML data, at the moment I can get the one to work just fine.

The XML that I need to manipulate looks like this;

<?xml version="1.0" encoding="UTF-8"?><Response><Errors><Error><Code>
			Something</Code><Message>
			A Message.</Message></Error></Errors><RequestID>
		ddf29ddf-8c43-4cg5-b854-22e3a9f1fa3f</RequestID></Response>
I am new to the XML scene so any help would be appreciative.

Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>