Proveniebam
Proveniebam

Reputation: 47

XPATH to elements

I'm trying to get the list of tblActions from an xmlfile but keep getting nothing.

<?xml version="1.0" standalone="yes"?>
<SecurityDS xmlns="http://tempuri.org/SecurityDS.xsd">
  <tblActiveDirectoryConfig>
    <AuthenticationEnabled>0</AuthenticationEnabled>
    <SSOEnabled>0</SSOEnabled>
    <DefaultLoginDomain />
  </tblActiveDirectoryConfig>
  <tblAccountPolicy>
    <PolicyId>1</PolicyId>
    <PasswordHistoryPeriod>6</PasswordHistoryPeriod>
    <MinPassword>3</MinPassword>
    <MaxPassword>30</MaxPassword>
    <LoginAttemptsAllowed>3</LoginAttemptsAllowed>
    <PasswordExpiration>60</PasswordExpiration>
  </tblAccountPolicy>
  <tblAction>
    <ActionID>1</ActionID>
    <ActionName>Adjust(w/ value)</ActionName>
    <ActionSeqID>10</ActionSeqID>
    <CreateDate>2002-07-15T17:49:18.1470000-05:00</CreateDate>
    <ModifyDate>2002-07-15T17:49:18.1470000-05:00</ModifyDate>
  </tblAction>
  <tblAction>
    <ActionID>2</ActionID>
    <ActionName>State0(ex. Unoccupied for BV, BO)</ActionName>
    <ActionSeqID>20</ActionSeqID>
    <CreateDate>2002-07-15T17:53:03.4900000-05:00</CreateDate>
    <ModifyDate>2002-07-15T17:53:03.4900000-05:00</ModifyDate>
  </tblAction>
  <tblAction>
    <ActionID>3</ActionID>
    <ActionName>State1(ex. Occupied for BV, BO)</ActionName>
    <ActionSeqID>21</ActionSeqID>
    <CreateDate>2002-07-15T17:53:14.4470000-05:00</CreateDate>
    <ModifyDate>2002-07-15T17:53:14.4470000-05:00</ModifyDate>
  </tblAction>
</<SecurityDS>

My Xpath I've tried:

My code snippet :

XmlNode root = fileContents.DocumentElement;

var nodes = root.SelectNodes(xpath_value);

In all cases I get zero nodes selected.

Upvotes: 0

Views: 130

Answers (1)

har07
har07

Reputation: 89295

Your XML has default namespace -namespace declared without prefix- here :

<SecurityDS xmlns="http://tempuri.org/SecurityDS.xsd">

Descendant element inherits ancestor default implicitly, unless otherwise specified. To access element in namespace when using XmlDocument class, you need to map a prefix to the namespace URI, and then use that prefix properly in the XPath :

var nsmgr = new XmlNamespaceManager(new NameTable());
//register mapping of prefix to namespace uri 
nsmgr.AddNamespace("d", "http://tempuri.org/SecurityDS.xsd");
//pass the namespace manager instance as 2nd param of SelectNodes():
var nodes = root.SelectNodes(".//d:tblAction", nsmgr);

Upvotes: 1

Related Questions