我的 LINQ 有什么问题吗?按照 MSDN 所说的去做,但是

发布于 2024-10-06 00:44:30 字数 1224 浏览 4 评论 0原文

我试图了解 LINQ,并从 MSDN 页面开始:

http: //msdn.microsoft.com/library/bb308960.aspx#xlinqoverview_topic2f

此处,它引用了一些示例 XML

<contacts>
  <contact>
    <name>Patrick Hines</name>
    <phone type="home">206-555-0144</phone>
    <phone type="work">425-555-0145</phone>
    <address>
      <street1>123 Main St</street1>
      <city>Mercer Island</city>
      <state>WA</state>
      <postal>68042</postal>
    </address>
    <netWorth>10</netWorth>
  </contact>
  <contact>...

,并表示以下内容将生成“phone”元素列表:

XElement contacts = XElement.Load(@"C:\Projects\ALL_MY_PROJECTS\LINQ_Noodling\UI\Contacts.xml");
foreach (x in contact.Elements("phone")) {
    Console.WriteLine(x);
}

如下所示:

<phone>206-555-0144</phone>
<phone>425-555-0145</phone>

除非它不。我什么也得不到。如果我用“联系人”替换上面的“电话”,我会得到所有联系人(包括“姓名”“电话”“地址”和所有子元素),但仅此而已。这并不是 MSDN 最后一次提供误导性或不正确的信息,但所提供的信息看起来是正确且合乎逻辑的。

I'm trying to get an understanding of LINQ, and starting out on the MSDN page:

http://msdn.microsoft.com/library/bb308960.aspx#xlinqoverview_topic2f

Here, it references some sample XML

<contacts>
  <contact>
    <name>Patrick Hines</name>
    <phone type="home">206-555-0144</phone>
    <phone type="work">425-555-0145</phone>
    <address>
      <street1>123 Main St</street1>
      <city>Mercer Island</city>
      <state>WA</state>
      <postal>68042</postal>
    </address>
    <netWorth>10</netWorth>
  </contact>
  <contact>...

and says that the following will pump out a list of "phone" elements:

XElement contacts = XElement.Load(@"C:\Projects\ALL_MY_PROJECTS\LINQ_Noodling\UI\Contacts.xml");
foreach (x in contact.Elements("phone")) {
    Console.WriteLine(x);
}

like this:

<phone>206-555-0144</phone>
<phone>425-555-0145</phone>

Except it doesn't. I get nothing whatever. If I replace "phone" in the above with "contact" I get all the contacts (including "name" "phone" "address" and all the child elements), but that is it. It wouldn't be the last time that MSDN provided misleading or incorrect information, but the info as presented looks correct and logical.

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

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

发布评论

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

评论(3

一梦浮鱼 2024-10-13 00:44:30

的子元素,不是根元素。如果您查看 MSDN 示例,请注意它们正在调用某个特定联系人元素上的元素。

您可以链接 Elements 调用*以获取更多子级:

foreach(XElement x in contacts.Elements("contact").Elements("phone"))

*如果我们很挑剔,它实际上并不是链接相同的函数,但 IEnumerable::Elements() 的行为与我们期望的非常相似无论如何。

<phone> is a child element of <contact>, not the root element. If you look at the MSDN examples, note that they're calling Elements on one particular contact element.

You can chain Elements calls* to get further children:

foreach(XElement x in contacts.Elements("contact").Elements("phone"))

*if we were being nitpicky, it's not really chaining the same function, but IEnumerable<XObject>::Elements() acts pretty much how we expect it to anyway.

一人独醉 2024-10-13 00:44:30

我认为在声称别人写的东西是错误的之前你应该多加注意。

他们所做的部分

foreach (x in contact.Elements("phone")) {
    Console.WriteLine(x);
}

前面是:

For example, you might have the following XML loaded into an XElement called contact:

<contact>
   Met in 2005.
   <name>Patrick Hines</name>
   <phone>206-555-0144</phone>
   <phone>425-555-0145</phone>
   <!--Avoid whenever possible-->
</contact>

并且您的 XML 文件与此不匹配。 Elements 仅查找元素的直接子元素。因此,使用您提供的 XML 文件,访问(文件中每个人的)电话号码的正确方法是

XElement contacts = XElement.Load(@"C:\Projects\ALL_MY_PROJECTS\LINQ_Noodling\UI\Contacts.xml");
foreach (x in contacts.Elements("contact").Elements("phone")) {
    Console.WriteLine(x);
}

I think you should pay more attention before claiming that something another person wrote is wrong.

The part in which they do

foreach (x in contact.Elements("phone")) {
    Console.WriteLine(x);
}

Is preceded by:

For example, you might have the following XML loaded into an XElement called contact:

<contact>
   Met in 2005.
   <name>Patrick Hines</name>
   <phone>206-555-0144</phone>
   <phone>425-555-0145</phone>
   <!--Avoid whenever possible-->
</contact>

And your XML file doesn't match that. Elements only finds direct children of the element. So using the XML file you provided, the correct way to access the phone numbers (of everyone in the file) would be

XElement contacts = XElement.Load(@"C:\Projects\ALL_MY_PROJECTS\LINQ_Noodling\UI\Contacts.xml");
foreach (x in contacts.Elements("contact").Elements("phone")) {
    Console.WriteLine(x);
}
两人的回忆 2024-10-13 00:44:30

您正在尝试访问联系人的子级的子级。您必须执行以下操作:

XElement contacts = XElement.Load(@"C:\Projects\ALL_MY_PROJECTS\LINQ_Noodling\UI\Contacts.xml");

foreach (x in contact.Elements("contact")) {
    Console.WriteLine(x.Element("phone"));
}

contacts 是 xml 的根,contact 是子项,phone的子项联系。循环中的 x 是一个 contact 节点,您需要在其上访问其子节点。

You are trying to access a child of a child of contacts. You'd have to do:

XElement contacts = XElement.Load(@"C:\Projects\ALL_MY_PROJECTS\LINQ_Noodling\UI\Contacts.xml");

foreach (x in contact.Elements("contact")) {
    Console.WriteLine(x.Element("phone"));
}

contacts is the root of the xml, contact is a child, and phone is a child of contact. The x in your loop is a contact node, on which you need to access its children.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文