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

发布于 2024-10-09 06:16:56 字数 1327 浏览 1 评论 0原文

我正在研究 Linq to 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 require ()、[] 或 {} 后键入...我做错了什么?

I am studying Linq to xml and i am reading a xml file using linq.. I used the following method,

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

and my xml file looks like this,

<?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>

and when i tried to execute the method i get an error

A new expression requires (), [], or {} after type... What am i doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何处潇湘 2024-10-16 06:16:56

你的代码应该是这样的:

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

You code should be something like this:

  XDocument xdoc = XDocument.Load(Path);
    var books = from b in xdoc.Elements("book")
                 select b; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文