结果查询中的值错误,因此Linq查询中出现Null Reference Exception

时间:2013-09-05 23:26:36

标签: c# linq

我有两个xml文件,我正在相互比较。正确执行result1后,Linq查询Null Reference Exception正在抛出rule。当我调试时,我发现section显示错误的值。我无法弄清楚原因。

Rules.xml文件:

<rule id="1" numberofsections="2">
  <section id="1" attributeid="1686" ruleoperator="=="  condition="and">
    <name>Processor type</name>
    <value>Core i3</value>
  </section>
  <section id="2" attributeid="1438" ruleoperator="&lt;" condition="and" >
    <name>Weight</name>
    <value>3.8 LBS</value>
  </section>
  <type>ultrabook</type>
</rule> 

代码段:

XDocument rulesXml = XDocument.Load("/RulesEnginescope/RulesEnginescope/rulesSubType.xml");
XDocument productXml = XDocument.Load("c:/RuleEngine/RuleEngine/product.xml");
var getSelectedLeafCategoryRules = from rules2 in       rulesXml.Descendants("QueryTransformation").Descendants("leafcategory")
                                       where ((long)System.Convert.ToDouble(rules2.FirstAttribute.Value) == 4590)
                                       select rules2;

    var rules = getSelectedLeafCategoryRules.Descendants("rule");
    var productAttribute = productXml.Descendants("AttrList").Descendants("Attr");

    foreach (var x in rules)
    {
        var section = x.Elements("section");          
       /*Wrong value in section.count()*/ 
        Console.WriteLine(section.Count());
       var result1 = from p in section
                      from pa in productAttribute
                      where (p.Attribute("attributeid").Value == pa.Attribute("id").Value
                       && p.Element("name").Value == pa.Element("Name").Value)
                      select new
                      {
                          ruleAttribute = new
                          {
                              ruleId = p.Attribute("attributeid").Value,
                              ruleOperator = p.Attribute("ruleoperator").Value,
                              name = p.Element("name").Value,
                              value = p.Element("value").Value,
                              condition = p.Attribute("condition").Value
                          },
                          prodAttribute = new
                          {
                              productId = pa.Attribute("id").Value,
                              name = pa.Element("Name").Value,
                              value = pa.Element("ValueList").Element("Value").Value
      /*Error*/                    }

                      };

        if (result1.Count() != 0 && result1.Count() == System.Convert.ToInt64(x.Attribute("numberofsections").Value))
        {
            //checking each section
            foreach (var r in result1)
            {
                ...
            }

    }

1 个答案:

答案 0 :(得分:2)

在LINQ-to-XML中获取元素和属性值的惯用方法是将元素或属性强制转换为所需的类型,而不是访问Value属性。

prodAttribute = new
                {
                   productId = (string)pa.Attribute("id"),
                   name = (string)pa.Element("Name"),
                   // ...
                }

使用此模式可避免在调用Attribute()Element()时找不到匹配节点时导致的空引用异常。它还减少了冗长:

((long)System.Convert.ToDouble(rules2.FirstAttribute.Value)
// should be 
(long)rules2.FirstAttribute

当您访问儿童的孩子时,您仍需要添加空检查。这可能会变得冗长;保持简洁的一种方法是使用面向IEnumerable的方法,这样你就可以在(可能是空的)集合上操作,而不是(可能是空的)实例。

pa.Element("ValueList").Element("Value").Value
// could be
(string)pa.Elements("ValueList").Elements("Value").FirstOrDefault ()

最后,请注意大写字母在LINQ-to-XML中很重要。在您的代码中,您似乎经常切换大写模式(“id”与“Name”);您的源XML可能更加一致。