在解析的Windows Phone 8的XML数据 [英] parse xml data in windows phone 8

查看:157
本文介绍了在解析的Windows Phone 8的XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML字符串正在添加构成Web服务器如下

 < soapenv:身体的xmlns:soapenv =HTTP ://schemas.xmlsoap.org/soap/envelope/> 
< doLoginResponse的xmlns =htt​​p://login.mss.uks.com>
< doLoginReturn>
<&的errorCode GT; IPH_I_LGN_002< /&的errorCode GT;
< ERRORMSG>在已记录成功地< / ERRORMSG>
<&号GT; 13733479454157901< /数字>
< / doLoginReturn>
< / doLoginResponse>
< / soapenv:身体与GT;



我想解析XML字符串,并想打印的errorCode,ERRORMSG,数量。我如何我能做到这一点。



先谢谢了。


解决方案

您可以使用的XDocument 来访问的元素在你的XML。下面的代码将打印的的errorCode ERRORMSG 的和的的在一个MessageBox元素:



<预类=郎-CS prettyprint-覆盖> 的XDocument DOC = XDocument.Parse(你的XML字符串);
变种的errorCode = doc.Descendants(XName.Get(的errorCode,http://login.mss.uks.com))FirstOrDefault();
变种ERRORMSG = doc.Descendants(XName.Get(ERRORMSG,http://login.mss.uks.com))FirstOrDefault();
变种数= doc.Descendants(XName.Get(号码,http://login.mss.uks.com))FirstOrDefault();

MessageBox.Show(的String.Format(错误代码:{0} \\\
Message:{1} \\\
Number:{2},errorCode.Value,errorMsg.Value,number.Value ));

这将显示与以下内容的消息框:




错误代码:IPH_I_LGN_002

消息:在成功地

号码登录:13733479454157901



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.

解决方案

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

这篇关于在解析的Windows Phone 8的XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆