我有从XML文件中提取数据的代码。我想找到每个提取值的平均值/平均值(XMax
,XMin
,YMax
,YMin
,ZMax
,{{1} })
以下是我如何提取六个值:
ZMin
我是否在XMax的平均值上走上正轨:
var query = from file in fileEntries
let doc = XDocument.Load(file)
let x = doc.Descendants("XAxisCalib").Single()
let y = doc.Descendants("YAxisCalib").Single()
let z = doc.Descendants("ZAxisCalib").Single()
select new
{
XMax = x.Element("Max").Value,
XMin = x.Element("Min").Value,
YMax = y.Element("Max").Value,
YMin = y.Element("Min").Value,
ZMax = z.Element("Max").Value,
ZMin = z.Element("Min").Value
};
答案 0 :(得分:0)
答案 1 :(得分:0)
var averageMaximumX = query.Average(t => t.XMax);
var averageMinimumX = query.Average(t => t.XMin);
var averageMaximumY = query.Average(t => t.YMax);
var averageMinimumY = query.Average(t => t.YMin);
var averageMaximumZ = query.Average(t => t.ZMax);
var averageMinimumZ = query.Average(t => t.ZMin);
编辑:将字符串转换为双精度或小数或其他:
var averageMaximumX = query.Average(t => double.Parse(t.XMax));
//OR: var averageMaximumX = query.Average(t => decimal.Parse(t.XMax));
但我实际上会在选择中执行此操作:
select new
{
XMax = double.Parse(x.Element("Max").Value),
XMin = double.Parse(x.Element("Min").Value),
// etc.
答案 2 :(得分:0)
这是破解你的答案的一种方法,但我根本不喜欢这个,我确信有一个正确的方法:
double aXMax = 0;
double aXMin = 0;
double aYMax = 0;
double aYMin = 0;
double aZMax = 0;
double aZMin = 0;
int count = (from file in fileEntries
let doc = XDocument.Load(file)
let x = doc.Descendants("XAxisCalib").Single()
let y = doc.Descendants("YAxisCalib").Single()
let z = doc.Descendants("ZAxisCalib").Single()
select new
{
aXMax += x.Element("Max").Value,
aXMin += x.Element("Min").Value,
aYMax += y.Element("Max").Value,
aYMin += y.Element("Min").Value,
aZMax += z.Element("Max").Value,
aZMin += z.Element("Min").Value
}).Count();
aXMax /= (double)count;
aXMin /= (double)count;
aYMax /= (double)count;
aYMin /= (double)count;
aZMax /= (double)count;
aZMin /= (double)count;
没有测试过,但它应该有效,即使它太丑了......