So i have this code parsing xml files. What I need it to do is check for values that are within the range and write out if it pass/fail. I'm not sure if my code: "where" statements are correct. I need the code to look for the "Max" and "Min" of each statement and to write out for example XMax the value is less than 287 it passed and greater than failed.
string[] fileEntries = Directory.GetFiles(@"c:\Sciclone UAC", "*.cfg*");
foreach (string fileName in fileEntries)
{
var query = from file in fileEntries
let doc = XDocument.Load(file)
let x = doc.Descendants("XAxisCalib").Single()
where (int)x.Attribute("Max") > 287
开发者_如何学编程 where (int)x.Attribute("Min") < -50
let y = doc.Descendants("YAxisCalib").Single()
where (int)y.Attribute("Max") > 645
where (int)y.Attribute("Min") > -87
let z = doc.Descendants("ZAxisCalib").Single()
where (int)z.Attribute("Max") > 20
where (int)z.Attribute("Min") > -130
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
};
Here is an example of what one xml file looks like:
<XAxisCalib>
<Max>281.68</Max>
<Min>-46.79</Min>
</XAxisCalib>
<YAxisCalib>
<Max>570.57</Max>
<Min>-123.24</Min>
</YAxisCalib>
<ZAxisCalib>
<Max>31.01</Max>
<Min>-100.95</Min>
Well your approach appear a little weak to me, because user can potentially mistakes and write rubbish instead of an int, so your where will fail. Isn't better to use a pure xsd approach and then validate the single entities ? As a personal experience, having an XSD to validat ethe simple xml, and a validation per entity lead to a easy to mantain and extend code, even if the inpuit is machine generated ( since sometimes specification changes ;) ) so I would prefer the old fashon way instead of linq to XML to solve such kind of situation, but is just my opinion coming from my experience.
精彩评论