未能根据我的要求编写LINQ
我有以下xml文件:未能根据我的要求编写LINQ,linq,linq-to-xml,Linq,Linq To Xml,我有以下xml文件: <?xml version="1.0" encoding="UTF-8"?> <!-- New XML document created with EditiX XML Editor (http://www.editix.com) at Tue Mar 18 22:41:05 IST 2014 --> <html xsi:NamespaceSchemaLocation="http://www.w3.org/1999/xhtml DM_Pro
<?xml version="1.0" encoding="UTF-8"?>
<!-- New XML document created with EditiX XML Editor (http://www.editix.com) at Tue Mar 18 22:41:05 IST 2014
-->
<html xsi:NamespaceSchemaLocation="http://www.w3.org/1999/xhtml DM_Project.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<head>
<title>title1</title>
</head>
<body>
<fragment name="heading" id="heading1">
<h1>Heading 1</h1>
</fragment>
<fragment name="heading" id="heading2">
<h2>Heading 2</h2>
</fragment>
<fragment name="paragraph" id="paragraph1">
<p>Paragraph 1</p>
</fragment>
</body>
</html>
正如预期的那样,其输出为:
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<p>Paragraph 1</p>
当我在LINQpad上运行此查询时,我得到以下错误:
Cannot execute text selection: Cannot convert type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute>' to 'string'
有人能指出我犯的错误吗
谢谢。试试这个:
var contents =
from content in xelement.Elements("body").Elements("fragment")
where content.Attribute("id").Value == "heading1"
select content;
您可以在where子句上重复where内容。嘿,感谢您指出了一个我非常愚蠢的错误:,但是,您的查询的输出将是:标题1,但是我想要的只是:标题1
Cannot execute text selection: Cannot convert type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute>' to 'string'
var contents =
from content in xelement.Elements("body").Elements("fragment")
where content.Attribute("id").Value == "heading1"
select content;