none
Alternate of XmlNodeList in 3.5 LINQ

    Question

  • Hi,

    What should be the alternate of XmlNodeList in 3.5 linq.

    Code snip;

    XmlNodeList xmlEntityTypeNodes;
    int intEntityCount;

    xmlTypeNodes = xmlImportDoc.SelectNodes("something");
        intEntityCount = xmlTypeNodes.Count;
        
        for(int i = intEntityCount - 1; 0 <= i; i--)
        {
           xmlNode = (XmlNode)xmlTypeNodes.Item(i);
           strNodeName = xmlNode.Attributes.GetNamedItem("Group").Value;
       }
    Monday, April 27, 2009 10:31 AM

Answers

  • I said IEnumerable<T> respectively IEnumerable<XElement>. That is quite different from IEnumerable which you seem to try to use.
    IEnumerable<T> has a Count method . However you don't need it to process nodes in reverse order, you can simply use the Reverse method, as already shown. There is not necessarily a 1:1 translation and even if there is one, it is not necessarily a good LINQ way to use it.


    MVP XML My blog
    Monday, April 27, 2009 11:12 AM
  • Thanks, today i have learn a new thing. GREAT
    • Marked as answer by PrashantSahni Tuesday, April 28, 2009 3:58 AM
    Monday, April 27, 2009 12:15 PM

All replies

  • LINQ generally operates on IEnumerable<T> like IEnumerable<XNode> or IEnumerable<XElement>.
    You seem to want your process your nodes in reverse order, here is an example with C# and LINQ to XML:

                XDocument doc = XDocument.Parse(@"<root>
      <foo>foo 1</foo>
      <foo>foo 2</foo>
      <foo>foo 3</foo>
    </root>");
                IEnumerable<XElement> foos = doc.Descendants("foo");
    
                foreach (XElement foo in foos.Reverse())
                {
                    Console.WriteLine((string)foo);
                }


    MVP XML My blog
    Monday, April 27, 2009 10:42 AM
  • But Count() method is not available with IEnumerable. If possible, can you just convert the above code using the IEnumerable.

    I have also tried to convert, below is the conversion, but i am not sure.

    XElement xmlEntityTypeNodes = xmlImportDoc.XPathSelectElement(

    "something");

    Int intEntityCount = xmlImportDoc.XPathSelectElements(

    "something").Count();

    for

     

    (int i = intEntityCount - 1; 0 <= i; i--)
         strNodeName = xmlEntityTypeNodes.Elements().ElementAt(i).Attribute("Group").Value;

    Monday, April 27, 2009 10:51 AM
  • As long as you can iterate the node list by foreach, you don't really need the count and use index to access each element. Try the following code:

    IEnumerable<XElement> xmlEntityTypeNodes = xmlImportDoc.XPathSelectElements("Something");
    foreach (XElement elem in foos.Reverse())
    {
        strNodeName = elem.Attribute("Group").Value;
    }


    Your potential, our passion.
    Monday, April 27, 2009 11:11 AM
  • I said IEnumerable<T> respectively IEnumerable<XElement>. That is quite different from IEnumerable which you seem to try to use.
    IEnumerable<T> has a Count method . However you don't need it to process nodes in reverse order, you can simply use the Reverse method, as already shown. There is not necessarily a 1:1 translation and even if there is one, it is not necessarily a good LINQ way to use it.


    MVP XML My blog
    Monday, April 27, 2009 11:12 AM
  • Thanks, today i have learn a new thing. GREAT
    • Marked as answer by PrashantSahni Tuesday, April 28, 2009 3:58 AM
    Monday, April 27, 2009 12:15 PM