Reputation: 784
I have a xml string comming form the web server as below
<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<doLoginResponse xmlns="http://login.mss.uks.com">
<doLoginReturn>
<errorCode>IPH_I_LGN_002</errorCode>
<errorMsg>Logged in sucessfully</errorMsg>
<number>13733479454157901</number>
</doLoginReturn>
</doLoginResponse>
</soapenv:Body>
I would like to parse the xml string and would like to print the errorCode, errorMsg, number . How I can I do it.
Thanks in advance.
Upvotes: 0
Views: 6351
Reputation: 6424
You can use XDocument
to access the elements in your XML. Following code will print errorCode, errorMsg and number elements in a MessageBox:
XDocument doc = XDocument.Parse("Your XML string");
var errorCode = doc.Descendants(XName.Get("errorCode", "http://login.mss.uks.com")).FirstOrDefault();
var errorMsg = doc.Descendants(XName.Get("errorMsg", "http://login.mss.uks.com")).FirstOrDefault();
var number = doc.Descendants(XName.Get("number", "http://login.mss.uks.com")).FirstOrDefault();
MessageBox.Show(String.Format("Error code: {0}\nMessage: {1}\nNumber: {2}", errorCode.Value, errorMsg.Value, number.Value));
This will show a MessageBox with following content:
Error code: IPH_I_LGN_002
Message: Logged in sucessfully
Number: 13733479454157901
Upvotes: 5