LINQ 到 XML。枚举没有结果
我在从 XML 文件填充对象时遇到问题。我复制了一个几乎完全相同的示例,更改了变量名称,但我不断收到“枚举未产生结果”异常。
这是我的代码:
Dim element As XElement = XElement.Load(path)
Dim itemProps = From p In element...<Property> _
Where p.<LanguageCode>.Value = "en_us" _
Select p.<Title>.Value, p.<Description>.Value
使用断点,我已确认使用 XElement.Load(path) 方法正确填充了“element”变量。
这是正在访问的 XML 文件:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Items>
<Item ItemID="1">
<Property ItemPropertyID="1">
<Title>Title1</Title>
<Description>Description1</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="2">
<Property ItemPropertyID="2">
<Title>Title2</Title>
<Description>Description2</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="3">
<Property ItemPropertyID="3">
<Title>Title3</Title>
<Description>Description3</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="4">
<Property ItemPropertyID="4">
<Title>Title4</Title>
<Description>Description4</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="5">
<Property ItemPropertyID="5">
<Title>Title5</Title>
<Description>Description5</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="6">
<Property ItemPropertyID="6">
<Title>Title6</Title>
<Description>Description6</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
</Items>
本质上, XML 查询应该返回每个属性的标题和描述,其中包含一个名为“语言代码”的元素,该元素等于“en-us”。我感觉我的问题出在我的 XML 代码上。
I'm having trouble populating an object from an XML file. I've copied an example I've found almost exactly, with variable names changed, but I keep getting the "Enumeration yielded no results" exception.
Here is my code:
Dim element As XElement = XElement.Load(path)
Dim itemProps = From p In element...<Property> _
Where p.<LanguageCode>.Value = "en_us" _
Select p.<Title>.Value, p.<Description>.Value
Using breakpoints, I have confirmed that the 'element' variable is being properly populated using the XElement.Load(path) method.
Here is the XML file that is being accessed:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Items>
<Item ItemID="1">
<Property ItemPropertyID="1">
<Title>Title1</Title>
<Description>Description1</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="2">
<Property ItemPropertyID="2">
<Title>Title2</Title>
<Description>Description2</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="3">
<Property ItemPropertyID="3">
<Title>Title3</Title>
<Description>Description3</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="4">
<Property ItemPropertyID="4">
<Title>Title4</Title>
<Description>Description4</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="5">
<Property ItemPropertyID="5">
<Title>Title5</Title>
<Description>Description5</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
<Item ItemID="6">
<Property ItemPropertyID="6">
<Title>Title6</Title>
<Description>Description6</Description>
<LanguageCode>en-us</LanguageCode>
</Property>
</Item>
</Items>
Essentially, the XML query is supposed to return the title and the description for every Property which has an element called Language Code, which is equal to "en-us". I have a feeling that my problem lies in my XML code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该语言代码:
应该是:
This language code:
should be:
中取出其中一个点
当您只需要向下 2 层时,请尝试从“向下 3 层”
。如果这不起作用,请尝试仅使用一个点,因为本质上您所行进的路径仅位于文档根目录下方的 1 处。
Try taking one of the dots out of
Your going 3 levels down, when you only need to go down 2.
If that doesn't work try just one dot, because essentially the path your travelling is only 1 below the root of the document.