Linq to XML Question

家住魔仙堡 提交于 2019-12-12 23:51:14

问题


Given the following XML, what query can I use to extract the value of preapprovalKey to a string variable? Still a little new to LINQ to XML.

  <?xml version="1.0" encoding="UTF-8" ?> 
- <ns2:PreapprovalResponse xmlns:ns2="http://svcs.paypal.com/types/ap">
- <responseEnvelope>
  <timestamp>2011-04-05T18:35:32.952-07:00</timestamp> 
  <ack>Success</ack> 
  <correlationId>7cec030fa3eb2</correlationId> 
  <build>1655692</build> 
  </responseEnvelope>
  <preapprovalKey>PA-9AG427954Y7578617</preapprovalKey> 
  </ns2:PreapprovalResponse>

回答1:


XDocument doc = XDocument.Load("test.xml");
string preapprovalKey = doc.Descendants("preapprovalKey").Single().Value;


来源:https://stackoverflow.com/questions/5560697/linq-to-xml-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!