Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,
How to Convert the XmlNodeList to List<XmlNode> without using for loop?
Thanks in advance
Posted

XmlNodeList to List<xmlnode> without using a loop:

var doc = new XmlDocument();
doc.LoadXml("<root><child /><child /></root>");
var xmlNodeList = doc.ChildNodes;
var nodees = new List<XmlNode>(xmlNodeList.Cast<XmlNode>());


hope it helps
 
Share this answer
 
Comments
Tarun.K.S 17-Nov-10 3:52am    
are you sure that it works?
i tried this in vb.net:
Dim nodes as New List(Of xmlNode)
nodes=xmlNodeList.Cast(Of xmlNode)()

but it gave the following exception:
Unable to cast object of type '<castiterator>d__aa`1[System.Xml.XmlNode]' to type 'System.Collections.Generic.List`1[System.Xml.XmlNode]'.
Tarun.K.S 17-Nov-10 4:03am    
i tried in c sharp and it worked perfectly!
Very good answer!!
Take a 5!


(got to check in vb.net :()
jim lahey 17-Nov-10 4:28am    
your VB is not equivalent to my C#.

Dim nodes as New List(Of xmlNode)(xmlNodeList.Cast(Of xmlNode)())

you need to pass the return value of the Cast method into the generic List constructor.
Tarun.K.S 20-Nov-10 5:20am    
Thanks a lot jim!
Laurence Yarosh 17-Jun-22 15:00pm    
This compiled and ran, where mds.SitesNode is an xml node.
var nodees = new System.Collections.Generic.List<xmlnode>(mds.SitesNode.ChildNodes.Cast<xmlnode>());
positionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionpositionposition
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900