如何转换< System.Xml.Linq.XElement>列出< string>

编程入门 行业动态 更新时间:2024-10-10 17:31:59
本文介绍了如何转换< 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; }

更多推荐

如何转换&lt; System.Xml.Linq.XElement&gt;列出&lt; string&gt;

本文发布于:2023-11-12 13:00:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:System   Xml   amp   lt   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!