LINQ 到 XML。枚举没有结果

发布于 2024-12-06 08:31:28 字数 2187 浏览 0 评论 0原文

我在从 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 技术交流群。

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

发布评论

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

评论(2

昇り龍 2024-12-13 08:31:28

该语言代码:

en_us

应该是:

en-us

This language code:

en_us

should be:

en-us
兰花执着 2024-12-13 08:31:28

中取出其中一个点

Dim itemProps = From p In element...<Property>

当您只需要向下 2 层时,请尝试从“向下 3 层”

。如果这不起作用,请尝试仅使用一个点,因为本质上您所行进的路径仅位于文档根目录下方的 1 处。

Try taking one of the dots out of

Dim itemProps = From p In element...<Property>

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.

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