我在LINQ语句中做错了什么?

时间:2010-12-27 04:37:51

标签: c# linq-to-xml

我正在研究Linq到xml,我正在使用linq读取一个xml文件..我使用了以下方法,

public void getbooklist(string Path)
{
    XDocument xdoc = XDocument.Load(Path);
    var books = new book in xdoc.Elements("book")
                 select book; 
}

和我的xml文件看起来像这样,

<?xml version="1.0" encoding="utf-8" ?>
<catalog>
  <book id="bk101">
     <author>Gambardella, Matthew</author>
     <title>XML Developer's Guide</title>
     <genre>Computer</genre>
     <price>44.95</price>
     <publish_date>2000-10-01</publish_date>
     <description>
      An in-depth look at creating applications
       with XML.
     </description>
  </book>
  <book id="bk102">
     <author>Ralls, Kim</author>
     <title>Midnight Rain</title>
     <genre>Fantasy</genre>
     <price>5.95</price>
     <publish_date>2000-12-16</publish_date>
     <description>
       A former architect battles corporate zombies,
       an evil sorceress, and her own childhood to become queen
       of the world.
    </description>
 </book>
</catalog>

当我尝试执行该方法时出现错误

A new expression requires (), [], or {} after type ......我做错了什么?

1 个答案:

答案 0 :(得分:5)

您的代码应该是这样的:

  XDocument xdoc = XDocument.Load(Path);
    var books = from b in xdoc.Elements("book")
                 select b;