xml 解析的通过/失败测试
所以我有这段代码解析 xml 文件。我需要它做的是检查范围内的值,并写出它是否通过/失败。我不确定我的代码:“where”语句是否正确。我需要代码来查找每个语句的“Max”和“Min”,并写出例如 XMax,该值小于 287,它通过了并且大于失败。
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
};
以下是一个 xml 文件的示例:
<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>
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>
好吧,你的方法对我来说似乎有点弱,因为用户可能会犯错误并写出垃圾而不是 int,所以你的位置将会失败。使用纯 xsd 方法然后验证单个实体不是更好吗?作为个人经验,使用 XSD 来验证简单的 xml,并且每个实体的验证都会导致易于维护和扩展的代码,即使输入是机器生成的(因为有时规范会发生变化;)),所以我更喜欢旧的时尚方式而不是 linq to XML 来解决这种情况,但这只是我根据我的经验得出的意见。
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.