LINQ to XML query returning no results
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</optHaulierDetCh开发者_如何学运维eck>
<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.
_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.
精彩评论