Is it possible to convert an XmlNodeList
to a List<string>
without declaring a new List<string>
?
I am looking for a simple implementation for this:
System.Xml.XmlNodeList membersIdList = xmlDoc.SelectNodes("//SqlCheckBoxList/value");
List<string> memberNames = new List<string>();
foreach (System.Xml.XmlNode item in membersIdList)
{
memberNames.Add(library.GetMemberName(int.Parse(item.InnerText)));
}
Yes, it's possible using LINQ:
var memberNames = membersIdList.Cast<XmlNode>()
.Select(node => node.InnerText)
.Select(value => int.Parse(value))
.Select(id => library.GetMemberName(id))
.ToList();
Cast<XmlNode>()
call is necessary, because XmlNodeList
does not implement generic IEnumerable<T>
, so you have to explicitly convert it to generic collection from non-generic IEnumerable
.
And yes, you can merge all Select
calls into one if you want:
var memberNames = membersIdList.Cast<XmlNode>()
.Select(x => library.GetMemberName(int.Parse(x.InnerText)))
.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With