Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

c# - Understanding Linq To Xml - Descendants return no results

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

alt text http://www.balexandre.com/temp/2010-02-26_0038.png

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...