2011-07-06 32 views
0

這是錯的嗎?請建議我正確的方式。如何分配結果(從LINQ到XML查詢)到列表(字符串)?

Public Function ReadXML() As List(Of String) 

    Dim list As New List(Of String) 
    Dim xmlDoc As XDocument = XDocument.Load("C:\\MappingFile.xml") 

    Dim q = From c In xmlDoc.Descendants("Entity") 
      Where c.Attribute("Source").Value = "E_cdclient" 
      Select New With { 
       .source = c.Elements("Property").Attributes("Source"), //This is one collection 
       .target = c.Elements("Property").Attributes("Target") //This is another collection. 
      } 

    list = q **//Here I am getting error.** 
    Return list 
End Function 

這是我的XML。

<?xml version="1.0" encoding="utf-8" ?> 
<Entities> 
    <Entity Source="E_cdclient" Target="cd_client"> 
    <Property Source="KnowledgeItemId" Target="CLIENT_CONTACT_ID"/> 
    <Property Source="KnowledgeClientID" Target="CLIENT_CONTACT_ID"/> 
    </Entity> 
    <Entity Source="E_cdclientsystem" Target="cd_client_system"> 
    <Property Source="PrimaryKnowledgeItemId" Target="0"/> 
    <Property Source="RelatedKnowledgeId" Target="0"/> 
    </Entity> 
    <Entity Source="E_cdclient_cdclientcontact" Target="cd_client_contact"> 
    <Property Source="shortdescription" Target="analysis_short_description"/> 
    <Property Source="OWNERID" Target="REF_PROJECT_OWNER_ID"/> 
    </Entity> 
    <Entity Source="E_cdclient_cdquestiontype" Target="cd_questiontype"> 
    <Property Source="" Target="analysis_short"/> 
    <Property Source="" Target="analysis_sho"/> 
    </Entity> 
</Entities> 

感謝, JN

+0

應該採取什麼結果列表中包含的對象?這是一個字符串列表,但是您正試圖爲它分配一組(匿名)對象。 – svick

回答

0

我不知道VB的,但我想,如果這是在C#ü不得不將其轉換爲列出所以你要LINQ結束查詢,你必須做這樣的事情(你的LINQ查詢).toList();

通過您返回(匿名)對象的方式,你想它assing串的名單,但返回類型不能爲字符串,因爲它實際上是用拖性質