有条件的文件解析,显示在屏幕上

发布于 2024-12-05 09:44:46 字数 1981 浏览 0 评论 0原文

我的代码目前正在构建,没有错误。它在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2024-12-12 09:44:46

我有点不清楚你在问什么,但你能做这样的事情吗?

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)

It's a little unclear to me what you're asking, but can you do something like this?

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 );
}

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:

    private static double TryParseWithDefault(string input, double defaultValue)
    {
        double result;
        if (!double.TryParse(input, out result))
            return defaultValue;
        return result;
    }

Which will at least not crash if the input is non-numeric. Then instead of

        Convert.ToInt32(item.XMax)

try using

        TryParseWithDefault(item.XMax, double.NaN)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文