我有一個用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
幫助?謝謝!
遺憾的XML並沒有在這裏呈現正確測量元素的結束標記丟失.. Measurement>。謝謝! – Sonya 2011-04-03 17:56:14
爲什麼你要創建一個新的'List',然後每次在'GetList'上調用'Add'?只需返回'D' ...如果你能提供一個簡短但完整的程序來證明它不需要所有的屬性,那麼這將是非常有幫助的,只是一對夫婦會好起來的。 –
2011-04-03 18:02:24
從粗略看一下你的代碼,它看起來很好 - 完全是什麼問題?沒有測量對象從XML文件中讀取? – Pandincus 2011-04-03 18:02:38