如何将结果(从LINQ到XML查询)分配给List(Of string)?

时间:2011-07-06 15:03:28

标签: xml vb.net linq

是不是错了?请建议我正确的方法。

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

1 个答案:

答案 0 :(得分:0)

我不知道VB,但我想如果它在c#中你必须将它转换为list,那么在你的LINQ查询结束时你必须做这样的事情(你的LINQ查询).toList();

顺便说一下,您返回(匿名)对象,并且您希望将其分配给字符串列表,但返回的类型不能是字符串,因为它实际上是具有两个属性的对象