2

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>
Mxyk
  • 10,678
  • 16
  • 57
  • 76
  • 1
    Is this a console app? Console.WriteLine will write output to the screen... – JohnD Sep 20 '11 at 12:35
  • yes console app. but wherehow do I write it to display the pass fail for each condition. I know console.writeline writes it to screen. –  Sep 20 '11 at 12:37

1 Answers1

2

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)
JohnD
  • 14,327
  • 4
  • 40
  • 53
  • That looks right. I want to print to screen whether the value of xmax xmin et cetra passed or failed meaning if it stays in the range set within the code. –  Sep 20 '11 at 12:58
  • Does the code above do what you want? If not, can you explain you want to see that it's not doing? – JohnD Sep 20 '11 at 13:02
  • Seems to me the above code should does state whether the conditions passed or failed. – Gage Sep 20 '11 at 13:14
  • 1
    @Casey, it looks like you are having some trouble parsing the XML values and converting them to ints. It is possible that some of the values are coming back as empty, or maybe non-numeric? If yes, then you should take a look at Int32.TryParse() which will return true/false if the string can be parsed. – JohnD Sep 20 '11 at 13:17
  • so instead of convert.toint32 it should be int32.tryparse? All of the values I'm trying to parse are numbers. I parsed them all to an excel file before now I just need to add a condition in them to parse only ones that fall in a range of values. –  Sep 20 '11 at 13:19
  • When it fails in debug what are the values of item? – Gage Sep 20 '11 at 13:21
  • It fails at that line and highlights the whole "select new { .....};" statements in yellow and says input string in incorrect format. I'm able to see that it did open one file and get the values out but it doesn't look like it initated the pass or fail part. It didn't like: Int32.TryParse() –  Sep 20 '11 at 13:26
  • So let me tell you the problem and go from their I have xml files (pasted what xml file looks like in original post) that I need to extract out values from. And those values I need to make sure their within a range and if they are they pass if not they fail and I want it to print out pass or fail for each condition. –  Sep 20 '11 at 13:45
  • @Casey, the values in the XML are non-integer (e.g. "-24.6")... I updated my answer to work with double numeric values. – JohnD Sep 20 '11 at 14:08
  • Shold this be line: TryParseWithDefault(item.XMax, double.NaN) be like this for each : TryParseWithDefault(item.XMax, double.NaN)< 290 ? "pass" : "fail" –  Sep 20 '11 at 14:26
  • 1
    Depends on what you are trying to do (you can change the default double.NaN to something else) but yes that is what I was proposing. You might need to change the constant "290" to "290.0" or cast to double. – JohnD Sep 20 '11 at 14:40
  • Wow Thanks. I had to cut out the console.writeline statements then paste back in and it works perfectly now. –  Sep 20 '11 at 14:56