2011-07-20 84 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