我的 LINQ 有什么问题吗?按照 MSDN 所说的去做,但是
我试图了解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是
的子元素,不是根元素。如果您查看 MSDN 示例,请注意它们正在调用某个特定联系人元素上的元素。您可以链接 Elements 调用*以获取更多子级:
*如果我们很挑剔,它实际上并不是链接相同的函数,但
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:
*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.我认为在声称别人写的东西是错误的之前你应该多加注意。
他们所做的部分
前面是:
并且您的 XML 文件与此不匹配。
Elements
仅查找元素的直接子元素。因此,使用您提供的 XML 文件,访问(文件中每个人的)电话号码的正确方法是I think you should pay more attention before claiming that something another person wrote is wrong.
The part in which they do
Is preceded by:
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您正在尝试访问
联系人
的子级的子级。您必须执行以下操作:contacts
是 xml 的根,contact
是子项,phone
是的子项联系
。循环中的x
是一个contact
节点,您需要在其上访问其子节点。You are trying to access a child of a child of
contacts
. You'd have to do:contacts
is the root of the xml,contact
is a child, andphone
is a child ofcontact
. Thex
in your loop is acontact
node, on which you need to access its children.