A.naam
A.naam

Reputation: 405

Trying to select single node returns the error "Expression must evaluate to a node-set."

I tried to select a single node using xpath, and it returned the error despite using [1] selector at the end of the path and XmlDocument.SelectSingleNode() function to get value. in my other web app it worked.

My xml file:

<settings>
  <GUI>
    <Theme>Dark</Theme>
  </GUI>
  <Mgmt>
    <NotAdmin>
      <ViewItems>1</ViewItems>
      <EditItems>1</EditItems>
      <DeleteItems>0</DeleteItems>
      <MgmtPanel>0</MgmtPanel>
      <EditDB>0</EditDB>
      <EditRestric>0</EditRestric>
    </NotAdmin>
  </Mgmt>
 </settings>

My C# code:

XmlDocument SettingsXMLdoc = new XmlDocument();
string svrSettingsPath = 
HostingEnvironment.MapPath("~/App_Data/AppSettings.xml");
SettingsXMLdoc.Load(svrSettingsPath);
XmlNode node = 
SettingsXMLdoc.SelectSingleNode(//(/settings/Mgmt/NotAdmin/ViewItems) 
[1]);
return node.InnerText; 

It should select the node but returns the error "Expression must evaluate to a node-set.".

Upvotes: 0

Views: 383

Answers (2)

Michael Kay
Michael Kay

Reputation: 163262

It's a very poor error message, but your expression is valid under XPath 2.0, but invalid under XPath 1.0. XPath 1.0 does not allow a parenthesized sub-expression after the "//" operator.

Upvotes: 1

Sk83r1l4m4
Sk83r1l4m4

Reputation: 153

This is working just fine for me:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Your_path_here);
XmlNode oneNode = xmlDoc.SelectSingleNode("settings/Mgmt/NotAdmin/ViewItems");

Upvotes: 1

Related Questions