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。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
_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 namedFields
of the XContainer_Xmlx
. If you have doneXDocument _Xmlx = XDocument("input.xml");
then your XContainer_Xmlx
has a sole descendant element namedFields
and your code would work. If you have doneXElement _Xmlx = XElement.Load("input.xml");
then the variable_Xmlx
is the "Fields" element itself.