如何将结果(从LINQ到XML查询)分配给List(字符串)? [英] How to assign result (from LINQ to XML query) to List(Of string) ?
本文介绍了如何将结果(从LINQ到XML查询)分配给List(字符串)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
这是错的吗?请建议我正确的方法.
Hi All,
Is it wrong? Please suggest me correct way.
Public Function ReadXML() As Lis<small>t(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
</small>
这是我的XML.
Here is my 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
Thanks,
JN
推荐答案
是的,这是错误的.投影形成为IEnumerable,并且必须使用ToList方法转换为List.同样,您的投影是一个由两个集合组成的对象,而不是字符串.
LINQ to XML [
Yes it is wrong. The projections is formed as an IEnumerable and must be cast to a List with the ToList method. Also your projection is an object composed of two collections, not a string.
LINQ to XML[^]
这篇关于如何将结果(从LINQ到XML查询)分配给List(字符串)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文