2011-07-20 58 views
0

我在VB中做一些XLINQ工作。基本上,我需要从XML的一小块拉一些值这里列出:LINQ to 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 

...离开出的实施对于每个循环....

所以我坚持每一个断点,收集没有它的元素。我错过了什么吗?

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

+1

你在哪里声明和初始化_Xmlx?那是XElement还是XDocument? –

+1

@ Martin Honnen:在上面,但它使用NDA代码,所以我不能显示它,它是一个XElement。我刚刚发现的这个不起作用。它需要是一个XDocument的权利?在回答中坚持原因,我会给你绿色的勾号。 – deanvmc

回答

1

_Xmlx.Descendants("Fields")寻找名为Fields的XContainer _Xmlx的后代元素。如果你已经完成了XDocument _Xmlx = XDocument("input.xml");那么你的XContainer _Xmlx有一个名为Fields的唯一后代元素,你的代码就可以工作。如果您已完成XElement _Xmlx = XElement.Load("input.xml");那么变量_Xmlx是“Fields”元素本身。

+0

干杯,这非常有意义! – deanvmc