[英]wrong value in result query so Null Reference Exception in Linq query
I have two xml files which I am comparing with each other. 我有两个xml文件,我正在相互比较。 The Linq query result1
is throwing Null Reference Exception
after executing correctly for one rule
. 在正确执行一个rule
后,Linq查询result1
正在抛出Null Reference Exception
。 And when I debugged I found the section
is displaying wrong values. 当我调试时,我发现该section
显示错误的值。 I am unable to figure out the cause. 我无法弄清楚原因。
Rules.xml file: 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="<" condition="and" >
<name>Weight</name>
<value>3.8 LBS</value>
</section>
<type>ultrabook</type>
</rule>
And the code snippet: 和代码片段:
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)
{
...
}
}
The idiomatic way to get the value of elements and attributes in LINQ-to-XML is to cast the element or attribute to the type you want, rather than accessing the Value
attribute. 在LINQ-to-XML中获取元素和属性值的惯用方法是将元素或属性强制转换为所需的类型,而不是访问Value
属性。
prodAttribute = new
{
productId = (string)pa.Attribute("id"),
name = (string)pa.Element("Name"),
// ...
}
Using this pattern avoids null ref exceptions caused when calls to Attribute()
and Element()
don't find a matching node. 使用此模式可避免在对Attribute()
和Element()
调用未找到匹配节点时导致的null ref异常。 It also reduces verbosity: 它还减少了冗长:
((long)System.Convert.ToDouble(rules2.FirstAttribute.Value)
// should be
(long)rules2.FirstAttribute
You'll still need to add null checks when you're accessing children of children. 当您访问孩子的孩子时,您仍然需要添加空检查。 This can get verbose; 这可能会变得冗长; one way to keep it succinct is to use IEnumerable-oriented methods so that you're operating on a (possibly empty) collection, rather than a (possibly null) instance. 保持简洁的一种方法是使用面向IEnumerable的方法,以便您在(可能是空的)集合上操作,而不是(可能为空)实例。
pa.Element("ValueList").Element("Value").Value
// could be
(string)pa.Elements("ValueList").Elements("Value").FirstOrDefault ()
Finally, note that capitalization matters in LINQ-to-XML. 最后,请注意大写字母在LINQ-to-XML中很重要。 In your code you seem to be switching capitalization patterns ("id" vs. "Name") often; 在您的代码中,您似乎经常切换大写模式(“id”与“Name”); it's likely that your source XML is more consistent. 您的源XML可能更加一致。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.