如何使用XML LINQ访问此XDocument

时间:2011-03-15 17:33:47

标签: c# xml

我有以下后代:

  <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" /> 

在同一个XDocument中有多个这些,但我循环使用以下内容:

IEnumerable<XElement> elements = XData.Descendants("ReportItem");

                foreach (XElement element in elements)

然后我尝试将这些分配给包含相同名称的业务对象类。 我想知道为什么这根本没有分配它们:

 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());
            }
        }

2 个答案:

答案 0 :(得分:1)

您正在使用错误的变量 - 您应该使用范围变量,但您正在使用外部循环变量。

所以这个:

select new DataPoint()
{
   Asset = element.Attribute("Asset").Value,
   ..

应该是

select new DataPoint()
{
  Asset = datapoint.Attribute("Asset").Value,
  ..

此外,您首先不需要外部循环,内部LINQ查询将覆盖不存在的属性 - 整个事情应该只是:

_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());

另请注意,您使用的是XML中未定义的属性(即MinutesIdleTripDistanceTripDistanceUnit)。在这种情况下,您要么测试并处理null,要么从DataPoint类中删除它们。处理null的一个例子是:

 MinutesIdle = (string)datapoint.Attribute("MinutesIdle") ?? "0",

答案 1 :(得分:0)

代码中的elements.Attributes("ReportItem")应该是什么?

我认为你真正想要的是:

_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
                                      }));