首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如果元素具有xmlns属性,则XDocument和Linq返回空。

如果元素具有xmlns属性,则XDocument和Linq返回空。
EN

Stack Overflow用户
提问于 2011-05-09 16:04:11
回答 3查看 10.5K关注 0票数 23

使用XDocuments和Linq的新手,请建议从xml字符串中的特定标记中检索数据的解决方案:

如果我有来自webservice响应的XML字符串(为了方便地格式化xml ):

代码语言:javascript
代码运行次数:0
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetCashFlowReportResponse xmlns="http://tempuri.org/">
      <GetCashFlowReportPdf>Hello!</GetCashFlowReportPdf>
    </GetCashFlowReportResponse>
  </soap:Body>
</soap:Envelope>

使用下面的代码,只有当GetCashFlowReportResponse标记没有"xmlns"属性时,我才能获得值。不知道为什么?否则,它总是返回null。

代码语言:javascript
代码运行次数:0
运行
复制
string inputString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><GetCashFlowReportResponse xmlns=\"http://tempuri.org/\"><GetCashFlowReportPdf>Hello!</GetCashFlowReportPdf></GetCashFlowReportResponse></soap:Body></soap:Envelope>"    
XDocument xDoc = XDocument.Parse(inputString);
//XNamespace ns = "http://tempuri.org/";
XNamespace ns = XNamespace.Get("http://tempuri.org/");

var data = from c in xDoc.Descendants(ns + "GetCashFlowReportResponse")
           select (string)c.Element("GetCashFlowReportPdf");

foreach (string val in data)
{
    Console.WriteLine(val);
}

我无法更改web服务以删除该属性。是否有更好的方法来读取响应并将实际数据返回给用户(以更易读的形式)?

编辑:解决方案:

代码语言:javascript
代码运行次数:0
运行
复制
XDocument xDoc = XDocument.Parse(inputString);
XNamespace ns = "http://tempuri.org/";

var data = from c in xDoc.Descendants(ns + "GetCashFlowReportResponse")
           select (string)c.Element(ns + "GetCashFlowReportPdf");
foreach (string val in data)
{
   Console.WriteLine(val);
}

注意:即使所有子元素都没有名称空间属性,如果将"ns“添加到元素中,代码也会工作,因为我猜childs继承了父元素的命名空间(参见SLaks的响应)。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-09 16:11:29

您需要包含名称空间:

代码语言:javascript
代码运行次数:0
运行
复制
XNamespace ns = "http://tempuri.org/";

xDoc.Descendants(ns + "GetCashFlowReportResponse")
票数 16
EN

Stack Overflow用户

发布于 2011-05-09 16:12:30

代码语言:javascript
代码运行次数:0
运行
复制
XName qualifiedName = XName.Get("GetCashFlowReportResponse", 
    "http://tempuri.org/");

var data = from d in xDoc.Descendants(qualifiedName)
票数 2
EN

Stack Overflow用户

发布于 2011-05-09 16:14:34

只需询问使用限定名的元素:

代码语言:javascript
代码运行次数:0
运行
复制
// create a XML namespace object
XNamespace ns = XNamespace.Get("http://tempuri.org/");

var data = from c in xDoc.Descendants(ns + "GetCashFlowReportResponse")
           select (string)c.Element(ns + "GetCashFlowReportPdf");

请注意重载+操作符的使用,该运算符创建具有XML命名空间和本地名称字符串的QName。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5939509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档