簡體   English   中英

結果查詢中的值錯誤,因此Linq查詢中出現Null Reference Exception

[英]wrong value in result query so Null Reference Exception in Linq query

我有兩個xml文件,我正在相互比較。 在正確執行一個rule后,Linq查詢result1正在拋出Null Reference Exception 當我調試時,我發現該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)
            {
                ...
            }

    }

在LINQ-to-XML中獲取元素和屬性值的慣用方法是將元素或屬性強制轉換為所需的類型,而不是訪問Value屬性。

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

使用此模式可避免在對Attribute()Element()調用未找到匹配節點時導致的null ref異常。 它還減少了冗長:

((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可能更加一致。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM