0

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

规则.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)
            {
                ...
            }

    }
4

1 回答 1

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 的方法,以便您在(可能为空的)集合上操作,而不是(可能为 null)实例。

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

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

于 2013-09-05T23:53:58.243 回答