1
Answer

XPath NodeIterator not able to iterate through results.

Ask a question
Lee Blake

Lee Blake

10y
805
1
I have some XML that is stored in the database and passed in as a string to my method.

    <settings>
      <ReportOptions>
        <ReportWidget>
          <widget Name="Report Exclusions">
            <Visibility>
              <VisibilitySetting Name="ClosedLocations" Value="true"/>
            </Visibility>
          </widget>
          <widget Name="Report Period">
            <Visibility>
              <VisibilitySetting Name="Day" Value="true"/>
              <VisibilitySetting Name="Month" Value="true"/>
            </Visibility>
          </widget>
          <widget Name="Output Method">
            <Properties>
              <Visibility>
                <VisibilitySetting Name="Report Viewer" Value="true"/>
                <VisibilitySetting Name="Pdf" Value="true"/>
              </Visibility>
            </Properties>
          </widget>
        </ReportWidget>
      </ReportOptions>
    </settings>

I run the following expression in an online XPath tester:

    /settings/ReportOptions/ReportWidget/widget[@Name='Output Method']/Properties/Visibility/VisibilitySetting[@Name]

and I get the following results:

    <?xml version="1.0" encoding="UTF-8"?>
    <result>
    <VisibilitySetting Name="Report Viewer" Value="true"/>
    
    <VisibilitySetting Name="Pdf" Value="true"/>
    
    </result>

But when I run the following code:

    var customizationApplied = false;
    var firstProperty = Items[0];
    var settings = firstProperty.Widget.Report.Settings;
    var widgetName = firstProperty.Widget.Name;
    
    // Select the node and place the results in an iterator
    var strExpression =     settings.Compile("/settings/ReportOptions/ReportWidget/widget[@Name='Output Method']/Properties/Visibility/VisibilitySetting[@Name]");
    
    
    var nodeIterator = settings.Select(strExpression);

    // iterate through the results
    while (nodeIterator.MoveNext())
    {
        var xmlPropertyName = nodeIterator.Current.GetAttribute("Name", string.Empty);

        var propertyToCustomize = (from prop in Items
                                   where string.Equals(prop.Name, xmlPropertyName, StringComparison.OrdinalIgnoreCase)
                                   select prop).FirstOrDefault();

        if (propertyToCustomize != null)
        {
            var xmlPropertyValue = nodeIterator.Current.GetAttribute("Value", string.Empty);
            propertyToCustomize.IsEnabled = StringToObjectHelper.GetValue(xmlPropertyValue, false);

            customizationApplied = true;
        } 
    }

I get no results. The nodeIterator always shows this on the mouseover: 

    Position=0, Current={Root}

and when I execute the nodeIterator.MoveNext() method, it returns false.

Can anyone please shed some light on what the problem could be? It all worked before and nothing has been changed.

Thanks,
Lee


Answers (1)
Next Recommended Forum