我想在那里值不使用一些文本的LINQ to XML!StartWith
我有这样的代码
IEnumerable<XElement> elements =
(from el in xmlFile.Root.Elements(elementName)
where (string)el.Attribute(attributeName) !StartWith("abc")
select el);
开始得到xml文件的所有属性值我怎样才能解决这个
我想在那里值不使用一些文本的LINQ to XML!StartWith
我有这样的代码
IEnumerable<XElement> elements =
(from el in xmlFile.Root.Elements(elementName)
where (string)el.Attribute(attributeName) !StartWith("abc")
select el);
开始得到xml文件的所有属性值我怎样才能解决这个
您需要使用有效的表达式,例如
where !el.Attribute(attributeName).Value.StartsWith("abc")
明白什么进入一个LINQ where
条款是非常重要的不是“魔术”语法 - 它只是一个普通的表达,这可以使用查询表达式(el
在这个声明的范围变量案件)。所以你应该问自己:“如果我不是在LINQ中写这个,而且有一个变量叫el
是指一个元素,我怎么会写一个if
条件来检查属性值是不是以abc
开头? “ (我使用明确的转换时,该属性可以丢失,我想只是得到一个null,但在这种情况下,你会去当属性的东西丢失,而且你只是想要的字符串值,所以你不妨使用Value
属性。)
注意,因为你已经只得到了where
条款这里(和琐碎select
),很可能会更具可读性使用非查询表达形式:
var elements = xmlFile.Root.Elements(elementName)
.Where(el => !el.Attribute(attributeName).Value.StartsWith("abc"));
试着用硫s(调整你的startwith)
IEnumerable<XElement> elements =
from el in xmlFile.Root.Elements(elementName)
where ! el.Attribute(attributeName).Value.StartWith("abc")
select el;
我得到这个错误: 对象引用未设置为对象的实例。 \t 它工作正常的另一种情况为: 其中(字符串)el.Attribute(attributeName)!= attributeValue select el – Maro 2012-07-25 08:29:07
@Maro:那么表明你有一个没有该属性的元素... – 2012-07-25 11:07:06