LINQ to XML查询返回没有结果

时间:2011-07-20 10:32:50

标签: vb.net linq-to-xml

我正在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。

1 个答案:

答案 0 :(得分:1)

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

相关问题