且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linq to XML-提取单个元素

更新时间:2022-11-27 23:09:49

您可以使用Descendants,然后使用FirstSingle-当前,您正在询问***元素它的正下方有一个SendDataResult元素,而实际上没有.此外,您没有使用正确的名称空间.这应该解决它:

You can use Descendants followed by First or Single - currently you're asking the top level element whether it's got a SendDataResult element directly beneath it, which it hasn't. Additionally, you're not using the right namespace. This should fix it:

XNamespace stuff = "http://stuff.com/stuff";
string data = responseXml.Descendants(stuff + "SendDataResult")
                         .Single()
                         .Value;

或者,直接导航:

XNamespace stuff = "http://stuff.com/stuff";
XNamespace soap = "http://www.w3.org/2003/05/soap-envelope";
string data = responseXml.Element(soap + "Body")
                         .Element(stuff + "SendDataResult")
                         .Value;