我有一个用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