Struggling with one aspect of my code that I hope someone could shed some light on.
I’m pulling an xml document multiple times from within a simple foreach loop. I want to append my linq query to a list, but it is rewriting the list each time. Here’s the code:
IEnumerable<TranList> tList;
foreach (var t in otherList)
{
//pulling xml data from service here - code not shown
XDocument xDoc = XDocument.Parse(xmlFromService);
tList = from x in xDoc.Descendants("Items")
select new TranList
{
BusDate = x.Descendants("BusDate").First().Value,
SeqNum = x.Descendants("SeqNum").First().Value,
Amount = x.Descendants("Amount").First().Value,
Credit = x.Descendants("Credit").First().Value
};
}
and here’s my xml for reference:
<Items>
<DbAmount>25,465.58</DbAmount>
<DBCount>296</DBCount>
<CrAmount>.00</CrAmount>
<CrCount>0</CrCount>
<Item>
<ID>0</ID>
<BusDate>20090126</BusDate>
<BlockNum>729</BlockNum>
<SeqNum>2</SeqNum>
<RelSeqNum>0</RelSeqNum>
<Serial />
<Routing>211690953</Routing>
<Account>123456789</Account>
<Amount>30.00</Amount>
<Currency>USD</Currency>
<Credit>DEBIT</Credit>
<Onus>TRANSIT</Onus>
</Item>
<Item>
. . . . . . . . . . . . . . .
</Item>
. . . . . . . . . . . . . . .
</Items>
Thanks for any help!
You are currently re-assigning
tListevery time, instead concatenate:But why do you need a
foreachin the first place? You are not even using the loop variable currently so this doesn’t make much sense as is.