xml 解析的通过/失败测试

发布于 12-05 03:28 字数 1812 浏览 0 评论 0原文

所以我有这段代码解析 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箹锭⒈辈孓2024-12-12 03:28:31

好吧,你的方法对我来说似乎有点弱,因为用户可能会犯错误并写出垃圾而不是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文