Thursday, 15 April 2010

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



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

i'm completly new linq2xml code much lines perform simple things, , in simple project wanted give try...

i'm 2 hours , nil get's right :(

i'm really, thinking go xmlnode-code-alike

the task:

i send soap action asmx service , response xml i parse xml xdocument object i seek list of nodes ... err! problem!

as can see screenshot

my xdocument has node called transactioninformationtype witch sequence, , simple want , retrieve 2 variables need (you can see code commented) below select c;

in watch window can see that

doc.descendants("transactioninformationtype")

returns nil @ all, , seeing content of xdocument in text visualizer, exist!

anyone care explain , help me passing huge wall?

thank you!

added

xdocument content

answer

the response xml has

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

and must utilize namespace!

turns out that, retrieve values, need use xnamespace well, final code looks this:

// parse xml xdocument doc = xdocument.parse(strresponse); xnamespace ns = "https://ssl.ditonlinebetalingssystem.dk/remote/payment"; var trans = item in doc.descendants(ns + "transactioninformationtype") select new transactioninformationtype { capturedamount = convert.toint32(item.element(ns + "capturedamount").value), orderid = item.element(ns + "cardtypeid").value };

thank all help!

var result = doc.descendants("transactioninformationtype");

selects descendants in xdocument have element name "transactioninformationtype" , in empty namespace. screenshot seems element you're trying select in namespace "https://ssl.ditonlinebetalingssystem.dk/remote/payment" though. need specify explicitly:

xnamespace ns = "https://ssl.ditonlinebetalingssystem.dk/remote/payment"; ↑↑ ↑ var result = doc.descendants(ns + "transactioninformationtype");

c# linq-to-xml

No comments:

Post a Comment