Linq to XML问题[英] Linq to XML Question

本文是小编为大家收集整理的关于Linq to XML问题的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

鉴于以下 XML,我可以使用什么查询将 preapprovalKey 的值提取到字符串变量?对 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>

推荐答案

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

本文地址:https://www.itbaoku.cn/post/1556871.html

问题描述

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>

推荐答案

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