有条件的文件解析,显示在屏幕上
我的代码目前正在构建,没有错误。它在 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>
My code is currently building with no errors. It is searching in an xml file for values, and I need for it to check if the values are within a range that determines if they pass/fail. I believe I have the code right but I need the pass/fail to display on the screen. Any help?
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",
};
Sample Xml: (more lines but deleted for simplicity)
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有点不清楚你在问什么,但你能做这样的事情吗?
更新:如果问题是您无法将值解析为整数,则需要添加一些错误处理。您的要求可能有所不同,但作为示例,您可以尝试像这样的简单方法:
如果输入是非数字,则至少不会崩溃。然后
尝试使用
It's a little unclear to me what you're asking, but can you do something like this?
Update: if the problem is that you can't parse the values to integer, you will need to add some error handling. Your requirements might be different, but as an example you can try a simple method like this:
Which will at least not crash if the input is non-numeric. Then instead of
try using