LINQ的认识到XML - 后裔返回任何结果 [英] Understanding Linq To Xml - Descendants return no results

查看:97
本文介绍了LINQ的认识到XML - 后裔返回任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个完全地新来Linq2XML我code到很多线进行简单的事情,并在一个简单的项目,我想试试看...

我与这2小时,并没有什么我得到的是正确的:(

我真的,真的想回去的 XmlNode- code-都

的任务:


  • 我发送SOAP行动ASMX服务,我也得到了响应,XML

  • 我解析XML转换为XDocument对象

  • 我试图让一个节点列表...犯错!问题!

因为你可以从看到这个截图

我的的XDocument 有一个叫做节点的 TransactionInformationType 女巫是一个序列,我简单的想要得到所有和检索只有2变数,我需要(你可以看到code评论)下方的选择C;

关注窗口中,您可以看到

  doc.Descendants(TransactionInformationType)

返回什么都没有,并由的XDocument在展示台文本内容看,它确实存在!

有人在乎解释和帮助我通过这个巨大的墙?

感谢您!


添加


  

的XDocument内容



响应XML有


  

< gettransactionlistResponse的xmlns =htt​​ps://ssl.ditonlinebetalingssystem.dk/remote/payment>


和我必须用这个作为命名空间!

事实证明,为检索值,我确实需要为使用的XNamespace以及,所以最终code是这样的:

  //解析XML
DOC的XDocument = XDocument.Parse(strResponse);
NS的XNamespace =htt​​ps://ssl.ditonlinebetalingssystem.dk/remote/payment;从项目VAR反=在doc.Descendants(NS +TransactionInformationType)
            选择新TransactionInformationType
            {
                capturedamount = Convert.ToInt32(item.Element(NS +capturedamount)。值)
                订单id = item.Element(NS +cardtypeid)。价值
            };


谢谢大家的帮助!


解决方案

  VAR的结果= doc.Descendants(TransactionInformationType);

选择在XDocument有元素名称TransactionInformationType,并在空命名空间中所有后代。
从你截图看来你要选择的元素在命名空间虽然https://ssl.ditonlinebetalingssystem.dk/remote/payment
您需要明确指定:

 的XNamespace NS =htt​​ps://ssl.ditonlinebetalingssystem.dk/remote/payment;
                                              ↑↑↑
VAR的结果= doc.Descendants(NS +TransactionInformationType);

I'm a completly New to Linq2XML as I code to much lines to perform simple things, and in a simple project I wanted to give it a try...

I'm with this for 2 hours and nothing I do get's it right :(

I'm really, really thinking to go back to XmlNode-code-alike

The Task:

  • I send a SOAP Action to an ASMX service and I get the response as XML
  • I Parse the XML into a XDocument object
  • I try to get a list of nodes ... err! Problem!

as you can see from this screenshot

my XDocument has a Node called TransactionInformationType witch is a Sequence, and I simple want to get all and retrieve the only 2 variables that I need (you can see the code commented) just below select c;

in the Watch window you can see that

doc.Descendants("TransactionInformationType")

returns nothing at all, and seeing by the content of the XDocument in the Text Visualizer, it does exist!

Anyone care to explain and help me passing this HUGE wall?

Thank you!


Added

XDocument content


Answer

the Response XML has

<gettransactionlistResponse xmlns="https://ssl.ditonlinebetalingssystem.dk/remote/payment">

and I must use this as Namespace!

turns out that, to retrieve values, I do need to use the XNamespace as well, so the final code looks like this:

// Parse XML
XDocument doc = XDocument.Parse(strResponse);
XNamespace ns = "https://ssl.ditonlinebetalingssystem.dk/remote/payment";

var trans = from item in doc.Descendants(ns + "TransactionInformationType")
            select new TransactionInformationType
            {
                capturedamount = Convert.ToInt32(item.Element(ns + "capturedamount").Value),
                orderid = item.Element(ns + "cardtypeid").Value
            };


Thank you all for the help!

解决方案

var result = doc.Descendants("TransactionInformationType");

selects all descendants in the XDocument that have element name "TransactionInformationType" and are in the empty namespace. From you screenshot it seems the element you're trying to select is in the namespace "https://ssl.ditonlinebetalingssystem.dk/remote/payment" though. You need to specify that explicitly:

XNamespace ns = "https://ssl.ditonlinebetalingssystem.dk/remote/payment";
                                              ↑↑                      ↑
var result = doc.Descendants(ns + "TransactionInformationType");

这篇关于LINQ的认识到XML - 后裔返回任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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