2011-04-03 20 views
1

我有一个用API定义气象测量的类。我已经创建了类来将这些度量值存储在一个XML文件中。我已经能够写入XML文件,但无法读取它。我需要能够读取数据,然后通过构造函数实例化测量类,然后将这些实例添加到列表 下面是测量构造需要帮助使用LINQ:XML创建类的实例

public Measurement(string longit, string latitud, string cty, string pcode, DateTime dte, decimal huy, decimal pre, string windDir, int windSp) : base(longit, latitud, cty,pcode) 
    { 

     dte = date; 
     huy = humidity; 
     pre = precipitation;   
     windDir = windDirection; 
     windSp = windSpeed; 
    } 

,这里的代码是的一个样本XML文件

<Measurement> 
<longitude>-76.2858726</longitude> 
<latitude>36.8507689</latitude> 
<city>Thetford</city> 
<postcode>IP24 1ED</postcode> 
<date>01/04/2011</date> 
<windDirection>SE</windDirection> 
<windSpeed>8</windSpeed> 
<humidity>2.6</humidity> 
<precipitation>0.0</precipitation> 
</Measurement> 

这里是我写的读取文件的方法,并创建类Measurement的实例。我收到没有错误,但没有设法创建列表<>

public List<Measurement> GetList() 
    { 
     List<Measurement> measurements = new List<Measurement>(); 
     XDocument doc = XDocument.Load(@"C:\Users\Sonya\Documents\Visual Studio 2010\Projects\WeatherAPI\WeatherAPI\measurement_store.xml"); 
     XElement root = doc.Root; 

    var d = (from s in root.Descendants("Measurement") 
       select new Measurement 
       { 
        Longitude = s.Element("longitude").Value, 
        Latitude = s.Element("latitude").Value, 
        City = s.Element("city").Value, 
        Postcode = s.Element("postcode").Value, 
        Date = DateTime.Parse(s.Element("date").Value), 
        Humidity = decimal.Parse(s.Element("humidity").Value), 
        Precipitation = decimal.Parse(s.Element("precipitation").Value), 
        WindSpeed = Convert.ToInt32(s.Element("windSpeed").Value), 
        WindDirection = s.Element("windDirection").Value, 

       }).ToList(); 


    d.ForEach(measurements.Add); 

     return measurements; 
    }//end GetList() 

我也写的另一种方法,一个稍微不同的格式..

public List<Measurement> createXMLListMeasurements() 
    { 
     //load the xml document 
     XDocument doc = XDocument.Load(@"C:\Users\Sonya\Documents\Visual Studio 2010\Projects\WeatherAPI\WeatherAPI\measurement_store.xml"); 
     //XElement root = doc.Root; 

     //instantiate a new list of measurements 
     List<Measurement> measurements = new List<Measurement>(); 

      //get a list of measurement elements 
      var d = from s in doc.Descendants("Measurement") 
        //where 
        select new 
        { //assign Measurement variables to data from xml doc 
         Longitude = (string)s.Element("longitude").Value, 
         Latitude = (string)s.Element("latitude").Value, 
         City = (string)s.Element("city").Value, 
         Postcode = (string)s.Element("postcode").Value, 
         Date = DateTime.Parse(s.Element("date").Value), 
         Humidity = decimal.Parse(s.Element("humidity").Value), 
         Precipitation = decimal.Parse(s.Element("precipitation").Value), 
         WindSpeed = Convert.ToInt32(s.Element("windSpeed").Value), 
         WindDirection = (string)s.Element("windDirection").Value, 
        }; 

      foreach (var s in d) 
      { //for each element found, instantiate Measurements class, and add to the measurements list. 
       Measurement m = new Measurement(s.Longitude, s.Latitude, s.City, s.Postcode, s.Date, s.Humidity, s.Precipitation, s.WindDirection, s.WindSpeed); 
       measurements.Add(m); 

      } 


     return measurements; 
    } 

道歉,如果这些问题看起来很可笑,LINQ和XML非常新,所以找到我的方式非常缓慢..任何帮助非常感谢!控制台应用程序调用此方法进行测试,并且只产生 WeatherAPI.Measurement WeatherAPI.Measurement

帮助?谢谢!

+0

遗憾的XML并没有在这里呈现正确测量元素的结束标记丢失..。谢谢! – Sonya 2011-04-03 17:56:14

+0

为什么你要创建一个新的'List ',然后每次在'GetList'上调用'Add'?只需返回'D' ...如果你能提供一个简短但完整的程序来证明它不需要所有的属性,那么这将是非常有帮助的,只是一对夫妇会好起来的。 – 2011-04-03 18:02:24

+0

从粗略看一下你的代码,它看起来很好 - 完全是什么问题?没有测量对象从XML文件中读取? – Pandincus 2011-04-03 18:02:38

回答

1

总的来说,你的代码看起来不错。正如Jon Skeet在他的评论中指出的那样,您不必费心将每个元素添加到列表中 - 您可以在调用.ToList()之后简单地返回查询结果。

很有可能你的xml文件有问题,或者你读的不正确。

如果您的XML文件是真正公正:

<Measurement> 
    <longitude>-76.2858726</longitude> 
    <latitude>36.8507689</latitude> 
    <city>Thetford</city> 
</Measurement> 

那么你的代码将无法正常工作,因为XML文件的测量。因此,拨打doc.Root.Descendants("Measurement")会给你0个结果。相反,你的xml文件应该有一个唯一的根元素,例如:

<root> 
    <Measurement> 
     <longitude>-76.2858726</longitude> 
     <latitude>36.8507689</latitude> 
     <city>Thetford</city> 
    </Measurement> 
    <Measurement> 
     <longitude>-71.2858726</longitude> 
     <latitude>32.1507689</latitude> 
     <city>Other City</city> 
    </Measurement> 
</root> 

而且,你不必费心获取XML文档的Root。如果你想要做的只是找到名为Measurement的元素,就说doc.Descendants("Measurement")

试试这个代码与上面的XML文件:我在LINQPad运行

void Main() 
{ 
    var measurements = GetMeasurements(@"C:\path\to\file\measurements.xml"); 
} 

public List<Measurement> GetMeasurements(string path) 
{ 
    XDocument doc = XDocument.Load(path); 

    return (from s in doc.Descendants("Measurement") 
      select new Measurement 
      { 
       Longitude = Convert.ToDecimal(s.Element("longitude").Value), 
       Latitude = Convert.ToDecimal(s.Element("latitude").Value), 
       City = s.Element("city").Value, 
      }).ToList(); 
} 

public class Measurement 
{ 
    public decimal Longitude { get; set; } 
    public decimal Latitude { get; set; } 
    public string City { get; set; } 
} 

,我得到以下结果:

sample result

+0

感谢Pandicus,我已经完全运行上面的代码,仍然得到相同的结果,显示“WeatherAPI.Measurement”的控制台窗口。我以前的文档有一个根,为根,为后代。还有什么想法?非常感谢!! – Sonya 2011-04-04 06:59:03

+0

@Pandicus,非常感谢你,修正它,有一点金发的时刻..没有正确显示我的结果,但是当我停止尝试将列表添加到列表中时,所有结果都没有问题。再次感谢!! – Sonya 2011-04-04 10:45:18

+0

@Sonya - 没问题 - 很高兴帮助! – Pandincus 2011-04-04 11:16:35