parse xml files and perform pass/fail test --Linq to xml?
So what I need to do is parse xml files and extract out data from the file and with the value I extract out I need to have the code determine whether it passed or failed the test.
for example: I extract out XMax and it needs to be less than 200 and if it is it passed if its higher it failed. I extract out XMin and it needs to be more than -200 and if it is it passed if its less it failed. I extract out YMax and it needs to be less than 200 and if it is it passed if its higher it failed. I extract out YMin and it needs to be more than -200 and if it is it passed if its less it failed.
here is what the xml file looks like with the values i need to check:
<XAxisCalib>
<Max>288.46</Max>
<Min>-46.6</Min>
</XAxisCalib>
<YAxisCalib>
<Max>646.76</Max>
<Min>-89.32</Min>
</YAxisCalib>
<ZAxisCalib>
<Max>19.24</Max>
<Min>-143.63</Min>
Anyone have suggestions on what to开发者_如何学JAVA you to write the program I was thinking linq to XML to parse the files and data out but now sure how I can compare the values.
Using LINQ to XML sounds fine to me, yes. Something like:
XDocument doc = XDocument.Load("file.xml");
int xMax = (int) doc.Root.Element("XMax");
int yMax = (int) doc.Root.Element("YMax");
int xMin = (int) doc.Root.Element("XMin");
int yMin = (int) doc.Root.Element("YMin");
return xMax < 200 &&
xMin > -200 &&
yMax < 200 &&
yMin > -200;
If that's not the sort of thing you were thinking of, please give more details.
EDIT: If the problem is having four different tests, you'd want something like:
public bool ValidateXMax(XDocument doc)
{
int xMax = (int) doc.Root.Element("XMax");
return xMax < 200;
}
... but repeated four times. Of course the repetition is slightly ugly, as is the hard-coding of the values, but it's unclear what you're trying to do, to be honest. Another alternative:
XDocument doc = XDocument.Load("file.xml");
int xMax = (int) doc.Root.Element("XMax");
int yMax = (int) doc.Root.Element("YMax");
int xMin = (int) doc.Root.Element("XMin");
int yMin = (int) doc.Root.Element("YMin");
bool xMaxValid = xMax < 200;
bool yMaxValid = yMax < 200;
bool xMinValid = xMin > -200;
bool xMinValid = yMin > -200;
精彩评论