-1

我想访问type值为的abc属性female

XElement xelement = XElement.Load("..\\..\\Employees.xml");
var name = from nm in xelement.Elements("Employee")
            where (string)nm.(Element("Abc") == "Female").Attribute("Type") == "Att"
            select nm;

这没有用。有什么办法让它发生吗?

4

3 回答 3

1

像这样的东西会起作用。不过,查看 Xml 会很有用。

var doc = XDocument.Load("c:\\temp\\test.xml");
var result = doc.Descendants("Employee")
                .Where(x=>(string)x.Value== "female")
                .Select(x=>x.Attribute("type").Value);

这是假设 xml 是这样的,查询将返回“foo1”。

<?xml version="1.0"?>
<root>-
   <Employee type="foo">
      <abc>male</abc>
   </Employee>
   <Employee type="foo1">
      <abc>female</abc>
   </Employee>
   <Employee type="foo2">
      <abc>male</abc>
   </Employee>
</root>
于 2013-07-17T14:26:47.323 回答
0

你的代码没有任何意义。

您不能嵌套比较和强制转换以及类似的对象。

相反,您需要使用&&运算符分别检查每个条件。

于 2013-07-17T14:22:29.197 回答
0

使用 XMLReader

来自 MSDN 的更多信息:http: //msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx

于 2013-07-17T14:28:55.250 回答