StefH / XPath2.Net

Lightweight XPath2 for .NET
Microsoft Public License
36 stars 14 forks source link

Selecting multiple results throws null exception on .NET Core #27

Closed Davidsv closed 5 years ago

Davidsv commented 5 years ago

The code below returns as expected on .NET framework:

<picture_url>url1</picture_url>
<picture_url>url2</picture_url>
<picture_url>url3</picture_url>

But in .NET Core, the first result is correct, then it throws a NullReferenceException on iterating to the next result.

<picture_url>url1</picture_url>
System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Xml.XPath.XNodeNavigator.MoveToNext(XPathNodeType type)
   at Wmhelp.XPath2.Iterator.ChildOverDescendantsNodeIterator.NextItem()
   at Wmhelp.XPath2.XPath2NodeIterator.MoveNext()
   at Wmhelp.XPath2.XPath2NodeIterator.Enumerator.MoveNext()
   at Wmhelp.XPath2.XNodeExtensions.XPath2Select(XNode node, XPath2Expression expression, Object arg)+MoveNext()

The code

void Main()
{
    XContainer c = XDocument.Parse(html);
    IEnumerable<Object> select = c.XPath2Select("//pictures/picture/picture_url");
    foreach(Object o in select)
    {
        Console.WriteLine(o);
    }
}

const String html = @"
         <pictures>
                  <picture>
                           <picture_url>url1</picture_url>
                  </picture>
                  <picture>
                           <picture_url>url2</picture_url>
                  </picture>
                  <picture>
                           <picture_url>url3</picture_url>
                  </picture>
         </pictures>";

Thoughts: There seems to be a switch on Clone() depending on the framework in https://github.com/StefH/XPath2.Net/blob/b4639abfa95fe7495209e4f0f54330d9c5ba5b9c/src/XPath2/XPath2NodeIterator.cs#L210, could it be the cause?

StefH commented 5 years ago

@Davidsv --> Can you please test version XPath2 1.0.10-ci-1876

from MyGet? https://www.myget.org/feed/xpath2/package/nuget/XPath2

Davidsv commented 5 years ago

That was fast :) It works.

StefH commented 5 years ago

New official NuGet will be released shortly.