Scott Grosch

I'm having a problem getting the elements I want via an XDocument query. I'm writing something like this:

Code Snippet

XElement el = doc.Element("OFX").Element("SIGNUPMSGSRSV1").Element("ACCTINFOTRNRS").Element("ACCTINFORS");

var acctinfo = from o in el.Elements("ACCTINFO")

select o;

The 'el' element is getting set to the value I expected, but acctinfo never has any elements. If I get inside VS and evaluate the el.Elements("ACCTINFO") I get null back. If I do Element("ACCTINFO") instead I get the first entry. What am I missing

Here's what VS shows in the debugger for the value of 'el'

doc.Element("OFX").Element("SIGNUPMSGSRSV1").Element("ACCTINFOTRNRS").Element("ACCTINFORS")
<ACCTINFORS>
 <DTACCTUP>20070820092248.609[-7Stick out tongueDT]</DTACCTUP>
 <ACCTINFO>
  <DESC>removed</DESC>
  <BANKACCTINFO>
   <BANKACCTFROM>
    <BANKID>removed</BANKID>
    <ACCTID>removed</ACCTID>
    <ACCTTYPE>removed</ACCTTYPE>
   </BANKACCTFROM>
   <SUPTXDL>Y</SUPTXDL>
   <XFERSRC>N</XFERSRC>
   <XFERDEST>Y</XFERDEST>
   <SVCSTATUS>ACTIVE</SVCSTATUS>
  </BANKACCTINFO>
 </ACCTINFO>
 <ACCTINFO>
  <DESC>removed</DESC>
  <BANKACCTINFO>
   <BANKACCTFROM>
    <BANKID>removed</BANKID>
    <ACCTID>removed</ACCTID>
    <ACCTTYPE>removed</ACCTTYPE>
   </BANKACCTFROM>
   <SUPTXDL>Y</SUPTXDL>
   <XFERSRC>Y</XFERSRC>
   <XFERDEST>Y</XFERDEST>
   <SVCSTATUS>ACTIVE</SVCSTATUS>
  </BANKACCTINFO> 
 </ACCTINFO>
 [More ACCTINFO block removed]
</ACCTINFORS>



Re: LINQ Project General Problems pulling XML elements

Ion Vasilian

You're probably not familiar with the lazy evaluation of LINQ expressions. The statement

var accinfo = from o in el.Elements("ACCTINFO") select o;

only formulates the query expression. In order to execute the query expression, you'll have to enumerate it. The foreach statement forces such enumeration. Try adding the following statement after the query expression:

foreach (XElement e in accinfo) Console.WriteLine(e);

It will print to the console all the elements as expected.

Another point. el.Elements("ACCTINFO") will never return null. It can however return an empty IEnumerable<XElement> if "el" doesn't have any "ACCTINFO" elements but that's not your case since el.Element("ACCTINFO") does return the first element.

Ion





Re: LINQ Project General Problems pulling XML elements

Scott Grosch

I'm following it with a foreach as you suggested and it's empty. That's my problem, never any data there Sad





Re: LINQ Project General Problems pulling XML elements

Ion Vasilian

Scott,

Given the query

XElement el = doc.Element("OFX").Element("SIGNUPMSGSRSV1").Element("ACCTINFOTRNRS").Element("ACCTINFORS");

you previously said that the line

Console.WriteLine(el.Element("ACCTINFO"));

outputs an element whereas the lines

var acctinfo = from o in el.Elements("ACCTINFO") select o;

foreach (XElement e in acctinfo) Console.WriteLine(e);

output nothing. This really shouldn't happen. Can you narrow down your scenario to a snippet that can be posted in the forum I would like to take a closer look at it.

Ion