我的代码目前正在构建,没有任何错误。它在xml文件中搜索值,我需要它来检查值是否在确定它们是否通过的范围内。我相信我的代码是正确的,但我需要通过/不能在屏幕上显示。有什么帮助吗?
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
};
var results = from item in query
select new
{
XMaxResult = Convert.ToInt32(item.XMax) < 290 ? "pass" : "fail",
XMinResult = Convert.ToInt32(item.XMin) > -50 ? "pass" : "fail",
YMaxResult = Convert.ToInt32(item.YMax) < 645 ? "pass" : "fail",
YMinResult = Convert.ToInt32(item.YMin) > -87 ? "pass" : "fail",
ZMaxResult = Convert.ToInt32(item.ZMax) < 20 ? "pass" : "fail",
ZMinResult = Convert.ToInt32(item.ZMin) > -130 ? "pass" : "fail",
};
示例Xml :(更多行但为简单起见而删除)
<XAxisCalib>
<Max>296</Max>
<Min>-51.04</Min>
</XAxisCalib>
<YAxisCalib>
<Max>640</Max>
<Min>-24.6</Min>
</YAxisCalib>
<ZAxisCalib>
<Max>29.67</Max>
<Min>-129</Min>
答案 0 :(得分:2)
我有点不清楚你在问什么,但你可以这样做吗?
foreach (var result in results)
{
Console.WriteLine("XMaxResult = {0}", result.XMaxResult );
Console.WriteLine("XMinResult = {0}", result.XMinResult );
Console.WriteLine("YMaxResult = {0}", result.YMaxResult );
Console.WriteLine("YMinResult = {0}", result.YMinResult );
Console.WriteLine("ZMaxResult = {0}", result.ZMaxResult );
Console.WriteLine("ZMinResult = {0}", result.ZMinResult );
}
更新:如果问题是您无法将值解析为整数,则需要添加一些错误处理。您的要求可能有所不同,但作为示例,您可以尝试这样的简单方法:
private static double TryParseWithDefault(string input, double defaultValue)
{
double result;
if (!double.TryParse(input, out result))
return defaultValue;
return result;
}
如果输入是非数字的,那么至少不会崩溃。然后而不是
Convert.ToInt32(item.XMax)
尝试使用
TryParseWithDefault(item.XMax, double.NaN)