简体   繁体   English

xml文件解析

[英]xml file parsing

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. 我如何添加/更改我的代码,所以如果提取出max / min的值,它会将提取的值与设定值进行比较并写出通过/失败。 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. 例如:xmax提取出来,它是280,我的代码中有一个条件,说xmax需要小于275,所以写出失败。 This needs to be done for each max/min.Any Ideas...? 这需要为每个最大/最小时间完成。任何想法......? I used linq to xml to parse but is their a better way? 我使用linq来解析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
                        };

Something like this maybe? 这样的事可能吗?

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

I'd use an XML schema for this. 我会为此使用XML模式。

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. 它将允许您构建C#,这样只需几行代码就可以检查文档的结构正确性和语义正确性。 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. 一些XML编辑器也支持模式,因此他们可能会在编辑器中获得某种自动语法支持/检查。

Here is how you would check a maximum value in an XSD schema: 以下是检查XSD架构中的最大值的方法:

http://www.w3schools.com/schema/schema_facets.asp 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>

...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM