2011-04-03 23 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