C#/MVC XML Parsing


  • 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;

                    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(requestUrl);
                    // Get the associated response for the above request.
                    using (HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse())
                catch (WebException e)
                    if (e.Status == WebExceptionStatus.ProtocolError)
                        var response = ((HttpWebResponse)e.Response);
                        ViewBag.httpstatuscode = response.StatusCode;
                        ViewBag.errorreceived = response.StatusDescription;
                            using (var stream = response.GetResponseStream())
                                using (var reader = new StreamReader(stream))
                                    string httpresponse = reader.ReadToEnd();
                                    XmlDocument xmlDoc = new XmlDocument();
                                    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"?>
    			A Message.
    I am new to the XML scene so any help would be appreciative.
    Saturday, June 08, 2013 9:05 AM


  •   var xele = XElement.Parse(@"<Response><Errors><Error><Code>Something</Code><Message>A Message.</Message>
                var items = (from temp in xele.Elements()
                            select new 
                                Code = temp.Element("Error").Element("Code").Value,
                                Message = temp.Element("Error").Element("Message").Value,
                                RequestId = temp.Element("RequestID").Value
    Let me know if you need more help.

    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo :, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Monday, June 10, 2013 5:28 AM