如何转换< System.Xml.Linq.XElement>列出< string> [英] how to convert <System.Xml.Linq.XElement> to List<string>
本文介绍了如何转换< System.Xml.Linq.XElement>列出< string>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友,
如何将linq查询转换为字符串列表
我的代码如下所示
Hi Friends,
How to convert linq query to a list of string
my code is given below
public static List<string> checksum(string cusip)
{
XDocument doc = XDocument.Load("SampleXML.xml");
List<string> aacruedlist = doc.Descendants("PositionSummary")
.Where(item =>
{
string cus = (string)item.Element("Cusip");
return cus != null && cus == cusip;
})
.ToList().Descendants("AccruedInterest").ToList();//error
return aacruedlist;
}
我收到错误
错误1无法将类型'System.Collections.Generic.List< System.Xml.Linq.XElement>'隐式转换为'System.Collections.Generic.List< string>'
请帮助
修改代码块 - OriginalGriff [/ edit]
Error i am getting
Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<System.Xml.Linq.XElement>' to 'System.Collections.Generic.List<string>'
please help
[edit]Code block fixed - OriginalGriff[/edit]
推荐答案
后代是XElement实例,而不是字符串,所以你需要选择Value属性才能得到字符串:
The Descendents are XElement instances, not strings, so you need to Select the Value property in order to get strings:
public static List<string> checksum(string cusip)
{
XDocument doc = XDocument.Load("SampleXML.xml");
List<string> aacruedlist = doc.Descendants("PositionSummary")
.Where(item =>
{
string cus = (string)item.Element("Cusip");
return cus != null && cus == cusip;
})
.Descendants("AccruedInterest")
.Select(d => d.Value)
.ToList();
return aacruedlist;
}
这篇关于如何转换< System.Xml.Linq.XElement>列出< string>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文