开发者

file parsing with a condition, display on screen

开发者 https://www.devze.com 2023-04-06 16:32 出处:网络
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

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"开发者_如何学Python,

                          }; 

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>


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号