xml文件解析

发布于 2024-12-05 22:05:37 字数 942 浏览 0 评论 0原文

如何添加/更改我的代码,以便一旦提取出最大/最小值的值,它将提取的值与设定值进行比较并写出通过/失败。例如:xmax 提取出来,它是 280,我的代码中有一个条件说 xmax 需要小于 275,所以写出失败。这需要为每个最大/最小完成。有什么想法......?我使用 linq to xml 进行解析,但它们是更好的方法吗?

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
                        };

How can I add/alter to my code so once if extracts out the values of the max/min it compare the extracted value to a set value and writes out pass/fail. For example: xmax extracts out and it is 280 and I have a condition in my code saying that xmax needs to be less than 275 so write out fail. This needs to be done for each max/min.Any Ideas...? I used linq to xml to parse but is their a better way?

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
                        };

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

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

发布评论

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

评论(3

无人问我粥可暖 2024-12-12 22:05:37

也许是这样的?

var results = from item in query
              select new 
              {
                   XMaxResult = item.XMax < 275 ? "pass" : "fail",
                   ...
              };

Something like this maybe?

var results = from item in query
              select new 
              {
                   XMaxResult = item.XMax < 275 ? "pass" : "fail",
                   ...
              };
漫漫岁月 2024-12-12 22:05:37

我将为此使用 XML 模式。

它将允许您构建 C#,这样只需几行代码即可检查文档的结构正确性和语义正确性。您也可以将其与应用程序分开发布,以便用户可以了解文档的布局方式。一些 XML 编辑器也支持模式,因此它们可能会在编辑器中获得某种自动语法支持/检查。

以下是检查 XSD 架构中最大值的方法:

http://www.w3schools.com /schema/schema_facets.asp

...

<xs:element name="Max">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <!-- Must be less than 275 -->
      <xs:maxInclusive value="274"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

...

I'd use an XML schema for this.

It will let you build your C# so that it takes just a few lines of code to check the document both for structural correctness, and semantic correctness. It is also something you can publish separately from your app so that users can understand how documents should be laid out. Some XML editors support schemas, too, so they might get some sort of automatic syntactical support/checking in their editor.

Here is how you would check a maximum value in an XSD schema:

http://www.w3schools.com/schema/schema_facets.asp

...

<xs:element name="Max">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <!-- Must be less than 275 -->
      <xs:maxInclusive value="274"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

...
夏日落 2024-12-12 22:05:37

您可以创建一个扩展方法来检查值

public static class Test {
static int check(this XElement element)
{
    if(element.Value < 100 || element.Value > 275)
    {
        throw new Exception();
    }

    return element.Value;
}
}

然后您可以执行以下操作

select new 
{
    XMax = x.Element("Max").Check(),
    XMin = x.Element("Min").Check(),
    YMax = y.Element("Max").Check(),
    YMin = y.Element("Min").Check(),
    ZMax = z.Element("Max").Check(),
    ZMin = z.Element("Min").Check()
};

You can create an Extension Method to check the value

public static class Test {
static int check(this XElement element)
{
    if(element.Value < 100 || element.Value > 275)
    {
        throw new Exception();
    }

    return element.Value;
}
}

Then you can do

select new 
{
    XMax = x.Element("Max").Check(),
    XMin = x.Element("Min").Check(),
    YMax = y.Element("Max").Check(),
    YMin = y.Element("Min").Check(),
    ZMax = z.Element("Max").Check(),
    ZMin = z.Element("Min").Check()
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文