Reputation: 2640
I'm getting my feet wet with linq to xml, and I have the data in memory but the following code is running without error, but without adding my objects to the data point list (the end of the procedure below). If I had to guess I'd say something is wrong with my querying, returning no nodes. Here's a sample of the xml:
<results>
<quote date="2012-02-07">
<Date>2012-02-07</Date>
<Open>44.76</Open>
<High>44.88</High>
<Low>44.22</Low>
<Close>44.60</Close>
<Volume>2547400</Volume>
<Adj_Close>44.60</Adj_Close>
</quote>
And here's my linq and relevant code:
List<IDataPoint> dataPointList = new List<IDataPoint>();
XDocument doc = XDocument.Load(AddressString);
var makeInfo =
from s in doc.Elements("quote")
where s.Element("Date") != null && s.Element("Open") != null
&& s.Element("High") != null && s.Element("Low") != null
&& s.Element("Close") != null && s.Element("Volume") != null
&& !s.Element("Open").Value.Equals("") && !s.Element("High").Value.Equals("")
&& !s.Element("Low").Value.Equals("") && !s.Element("Close").Value.Equals("")
select new DailyPricingVolDP(symbol, (DateTime)s.Element("Date"),
(double)s.Element("Open"),
(double)s.Element("High"),
(double)s.Element("Low"),
(double)s.Element("Close"),
(long)s.Element("Volume"));
foreach (var item in makeInfo)
{
dataPointList.Add(item);
}
Upvotes: 2
Views: 120
Reputation: 2640
I got it on my own but thanks for the quick help...
doc.Elements("quote")
needs to be:
doc.Descendants("quote")
and it works as expected. Thanks again..
Upvotes: 0
Reputation: 5799
I'm pretty sure XDocument.Elements() only returns direct children, and based on your XML doc.Elements("quote") will not match anything. Use XDocument.Decendants().
I.E. doc.Descendants("quote")
Upvotes: 1
Reputation: 59016
Try
var makeInfo =
from s in doc.Element("result").Elements("quote")
where s.Element("Date") != null && s.Element("Open") != null
&& s.Element("High") != null && s.Element("Low") != null
&& s.Element("Close") != null && s.Element("Volume") != null
&& !s.Element("Open").Value.Equals("") && !s.Element("High").Value.Equals("")
&& !s.Element("Low").Value.Equals("") && !s.Element("Close").Value.Equals("")
select new DailyPricingVolDP(symbol, (DateTime)s.Element("Date"),
(double)s.Element("Open"),
(double)s.Element("High"),
(double)s.Element("Low"),
(double)s.Element("Close"),
(long)s.Element("Volume"));
I'd also recommend writing, for example, s.Element("Open").Value != string.Empty
instead of !s.Element("Open").Value.Equals("")
, but that's a stylistic thing.
Upvotes: 0