Reputation: 4779
I have the following descendants:
<ReportItem Asset="111" ESN="111" Longitude="123" Latitude="123" MessageTime="2/16/2011" MessageTimeZone="CST" MessageTimeZoneGMTOffset="-360" SpeedUnit="Mph" Speed="111" Direction="West" Address="test" Name="testing" />
There are multiple of these in the same XDocument but I am looping through them with the following:
IEnumerable<XElement> elements = XData.Descendants("ReportItem");
foreach (XElement element in elements)
I am then trying to assigning these to a business objects class that contains the same names. I am wondering why this is not assigning them at all:
foreach (XElement element in elements)
{
_dataPoints.AddRange((from datapoint in elements.Attributes("ReportItem")
select new DataPoint()
{
Asset = element.Attribute("Asset").Value,
ESN = element.Attribute("ESN").Value,
Longitude = element.Attribute("Longitude").Value,
Latitude = element.Attribute("Latitude").Value,
MessageTime = element.Attribute("MessageTime").Value,
MessageTimeZone = element.Attribute("MessageTimeZone").Value,
MessageTimeZoneGMTOffset = element.Attribute("MessageTimeZoneGMTOffset").Value,
MinutesIdle = element.Attribute("MinutesIdle").Value,
Address = element.Attribute("Address").Value,
Name = element.Attribute("Name").Value,
TripDistance = element.Attribute("TripDistance").Value,
TripDistanceUnit = element.Attribute("TripDistanceUnit").Value,
}).ToList());
}
}
Upvotes: 0
Views: 259
Reputation: 161012
you are using the wrong variable - you should use the range variable but you are using the outer loop variable.
So this:
select new DataPoint()
{
Asset = element.Attribute("Asset").Value,
..
should be
select new DataPoint()
{
Asset = datapoint.Attribute("Asset").Value,
..
Also you do not need the outer loop in the first place, and the inner LINQ query goes over an attribute that doesn't exist - the whole thing should just be:
_dataPoints.AddRange((from datapoint in elements
select new DataPoint()
{
Asset = datapoint.Attribute("Asset").Value,
ESN = datapoint.Attribute("ESN").Value,
Longitude = datapoint.Attribute("Longitude").Value,
Latitude = datapoint.Attribute("Latitude").Value,
MessageTime = datapoint.Attribute("MessageTime").Value,
MessageTimeZone = datapoint.Attribute("MessageTimeZone").Value,
MessageTimeZoneGMTOffset = datapoint.Attribute("MessageTimeZoneGMTOffset").Value,
MinutesIdle = datapoint.Attribute("MinutesIdle").Value,
Address = datapoint.Attribute("Address").Value,
Name = datapoint.Attribute("Name").Value,
TripDistance = datapoint.Attribute("TripDistance").Value,
TripDistanceUnit = datapoint.Attribute("TripDistanceUnit").Value,
}).ToList());
Also note that you are using attributes that are not even defined in your XML (i.e. MinutesIdle
, TripDistance
, TripDistanceUnit
). You either have test and handle null in this case or remove those from your DataPoint
class. An example of handling null would be :
MinutesIdle = (string)datapoint.Attribute("MinutesIdle") ?? "0",
Upvotes: 1
Reputation: 60276
What is elements.Attributes("ReportItem")
in your code supposed to do?
I think what you really want is this:
_dataPoints.AddRange(XData.Descendants("ReportItem").Select(element =>
select new DataPoint(){
Asset = element.Attribute("Asset").Value,
ESN = element.Attribute("ESN").Value,
Longitude = element.Attribute("Longitude").Value,
Latitude = element.Attribute("Latitude").Value,
MessageTime = element.Attribute("MessageTime").Value,
MessageTimeZone = element.Attribute("MessageTimeZone").Value,
MessageTimeZoneGMTOffset = element.Attribute("MessageTimeZoneGMTOffset").Value,
MinutesIdle = element.Attribute("MinutesIdle").Value,
Address = element.Attribute("Address").Value,
Name = element.Attribute("Name").Value,
TripDistance = element.Attribute("TripDistance").Value,
TripDistanceUnit = element.Attribute("TripDistanceUnit").Value
}));
Upvotes: 0