locked
For each problem... RRS feed

  • Question

  • Hey all,

    I am trying do do a foreach loop on an xml document using the xmlDocument.SelectNodes("xPath"), but I get an error from the visual workflow editor:

    "Error 2 Compiler error(s) encountered processing expression "xDoc.DocumentElement.SelectNodes("path")".
    Option Strict On disallows implicit conversions from 'System.Xml.XmlNodeList' to 'System.Collections.Generic.IEnumerable(Of System.Xml.XmlNode)"

    ...  uh, I thought XmlNodeList implements IEnumerable<XmlNode>. 

    So anyways, I then tried to cast the xmlnodelist.. CType(xDoc.DocumentElement.SelectNodes("path"), IEnumerable(Of XmlNode))

    Which allowed it to build.  But then when it runs I get:  "Unable to cast object of type 'System.Xml.XPathNodeList' to type 'System.Collections.Generic.IEnumerable`1[System.Xml.XmlNode]'...

    I have checked and double checked and triple checked and it is an XmlNodeList.  Any ideas (I am not a VB guy so maybe there is some VB.NET nuance I don;t know about)?

    Thanks!

    Thursday, November 19, 2009 8:55 PM

Answers

  • ForEach<T> only allows iterating through IEnumerable<T> collections (generic collections). It does not allow iterating through non-generic collections like IEnumerable, IList, or in this case XmlNodeList (that implements IEnumerable).

    You can work around this using Linq. In you ForEach<XmlNode>, set the Values argument to the following expression:

    From

     

     

    node In xmlDoc.SelectNodes("yourPath").Cast(Of XmlNode)() Select node


    Leon Welicki
    Program Manager, WF

    Friday, November 20, 2009 8:41 AM