0

I get response from an api when i use it to send the sms using http request now i get some reponse when i send the sms . I need to parse "MessageID" from the reponse .

I am using following code to read the response

   HttpWebResponse webresponse = (HttpWebResponse)myReq.GetResponse();



        Encoding enc = System.Text.Encoding.GetEncoding(1252);
        StreamReader loResponseStream = new
          StreamReader(webresponse.GetResponseStream(), enc);

        string Response = loResponseStream.ReadToEnd();

What i recieve in string Response is following

<rsp stat=\"ok\">\n<success msg=\"accepted\" transactionid=\"fe417b1b3dd5f68cc99c5df182fe606a\" messageid=\"332b21a2813900a7b81af1635aa1a8d5\"/>\n</rsp>

How can parse value of transactionid and message id from this response . Please let me know . Thank You

1 Answer 1

2

You can use LINQ to XML:

  var successElement = XElement.Parse(response).Element("success");

  var transactionId = successElement.Attribute("transactionid").Value;
  var messageId = successElement.Attribute("messageid").Value;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it work but can you explain it a bit . and secondly when i was trying like follow it was not working xmlDocument.LoadXml(loResponseStream.ReadToEnd()); // string value = xmlDocument.GetElementsByTagName("messageid").ToString(); i was able to parse the xml by using INNERXML but not element tag LoadXml(xmlDocument.SelectSingleNode("rsp").InnerXml); -> it was working
Well, LINQ to XML is new toy from .NET 3.5 which supports you easily to query data from Xml. You can start in here: msdn.microsoft.com/en-us/library/bb387098.aspx. Otherwise, XmlDocument is an old kid which from .NET 1.1.