LINQ to XML 查询不返回结果

发布于 2024-11-25 02:52:08 字数 2089 浏览 0 评论 0原文

我正在用 VB 做一些 XLINQ 工作。我基本上需要从一小块 XML 中提取一些值,如下所示:

<?xml version="1.0" encoding="utf-8"?>
  <Fields>
    <typeQtyRadioButtonList>1</typeQtyRadioButtonList>
    <cmbQtyCheck>Reject</cmbQtyCheck>
    <optHaulierDetCheck>1</optHaulierDetCheck>
    <txtReasonCode>1</txtReasonCode>
    <optGenMod>0</optGenMod>
    <optFarmRestrictions>0</optFarmRestrictions>
    <cmbFRAction>Reject</cmbFRAction>
    <optDisease>0</optDisease>
    <txtDReasonCode>2</txtDReasonCode>
    <optWithdrawl>0</optWithdrawl>
    <txtWithdrawl>3</txtWithdrawl>
    <optABM>0</optABM>
    <txtCompliance>3</txtCompliance>
    <optForm>1</optForm>
  </Fields>

为此,我使用:

    Dim _ControlValueCollections = From _ControlValueCollection In _Xmlx.Descendants("Fields") _
                                  Select _Qstn1Response = _ControlValueCollection.Element("typeQtyRadioButtonList").Value, _
                                         _Qstn2Response = _ControlValueCollection.Element("optHaulierDetCheck").Value, _
                                         _Qstn3Response = _ControlValueCollection.Element("optGenMod").Value, _
                                         _Qstn4Response = _ControlValueCollection.Element("optFarmRestrictions").Value, _
                                         _Qstn5Response = _ControlValueCollection.Element("optDisease").Value, _
                                         _Qstn6Response = _ControlValueCollection.Element("optWithdrawl").Value, _
                                         _Qstn7Response = _ControlValueCollection.Element("optABM").Value, _
                                         _Qstn8Response = _ControlValueCollection.Element("optForm").Value

    For Each _ControlValueCollection In _ControlValueCollections

...省略 For Each 循环的实现...

所以我在对于每个,集合中没有元素。我错过了什么吗?

编辑:答案当然是我使用的是 XElement 而不是 XDocument。

I'm doing some XLINQ in VB for work. I basically need to pull some values from a small chunk of XML as listed here:

<?xml version="1.0" encoding="utf-8"?>
  <Fields>
    <typeQtyRadioButtonList>1</typeQtyRadioButtonList>
    <cmbQtyCheck>Reject</cmbQtyCheck>
    <optHaulierDetCheck>1</optHaulierDetCheck>
    <txtReasonCode>1</txtReasonCode>
    <optGenMod>0</optGenMod>
    <optFarmRestrictions>0</optFarmRestrictions>
    <cmbFRAction>Reject</cmbFRAction>
    <optDisease>0</optDisease>
    <txtDReasonCode>2</txtDReasonCode>
    <optWithdrawl>0</optWithdrawl>
    <txtWithdrawl>3</txtWithdrawl>
    <optABM>0</optABM>
    <txtCompliance>3</txtCompliance>
    <optForm>1</optForm>
  </Fields>

And to do this I am using:

    Dim _ControlValueCollections = From _ControlValueCollection In _Xmlx.Descendants("Fields") _
                                  Select _Qstn1Response = _ControlValueCollection.Element("typeQtyRadioButtonList").Value, _
                                         _Qstn2Response = _ControlValueCollection.Element("optHaulierDetCheck").Value, _
                                         _Qstn3Response = _ControlValueCollection.Element("optGenMod").Value, _
                                         _Qstn4Response = _ControlValueCollection.Element("optFarmRestrictions").Value, _
                                         _Qstn5Response = _ControlValueCollection.Element("optDisease").Value, _
                                         _Qstn6Response = _ControlValueCollection.Element("optWithdrawl").Value, _
                                         _Qstn7Response = _ControlValueCollection.Element("optABM").Value, _
                                         _Qstn8Response = _ControlValueCollection.Element("optForm").Value

    For Each _ControlValueCollection In _ControlValueCollections

... Leaving out the implementation of the For Each loop....

So I have stuck a break point on the for each and the collection has no elements in it. Am I missing something ?

EDIT: The Answer was of course that I was using an XElement and not an XDocument.

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

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

发布评论

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

评论(1

南笙 2024-12-02 02:52:08

_Xmlx.Descendants("Fields") 查找 XContainer _Xmlx 的名为 Fields 的后代元素。如果您已完成 XDocument _Xmlx = XDocument("input.xml");,那么您的 XContainer _Xmlx 有一个名为 Fields 的唯一后代元素,并且您的代码会起作用。如果您已执行 XElement _Xmlx = XElement.Load("input.xml");,则变量 _Xmlx 就是“Fields”元素本身。

_Xmlx.Descendants("Fields") looks for descendant elements named Fields of the XContainer _Xmlx. If you have done XDocument _Xmlx = XDocument("input.xml"); then your XContainer _Xmlx has a sole descendant element named Fields and your code would work. If you have done XElement _Xmlx = XElement.Load("input.xml"); then the variable _Xmlx is the "Fields" element itself.

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