元素的 XPATH
XPATH to elements
我正在尝试从 xml 文件中获取 tblActions 列表,但始终一无所获。
<?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>
我试过的 Xpath:
- .//tblAction
- //tblAction
- /SecurityDS/tblAction
我的代码片段:
XmlNode root = fileContents.DocumentElement;
var nodes = root.SelectNodes(xpath_value);
在所有情况下,我都选择了零个节点。
您的 XML 具有 默认命名空间 - 声明的命名空间没有前缀 - 此处:
<SecurityDS xmlns="http://tempuri.org/SecurityDS.xsd">
后代元素隐式继承祖先默认值,除非另有说明。要在使用 XmlDocument
class 时访问命名空间中的元素,您需要将前缀映射到命名空间 URI,然后在 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);
我正在尝试从 xml 文件中获取 tblActions 列表,但始终一无所获。
<?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>
我试过的 Xpath:
- .//tblAction
- //tblAction
- /SecurityDS/tblAction
我的代码片段:
XmlNode root = fileContents.DocumentElement;
var nodes = root.SelectNodes(xpath_value);
在所有情况下,我都选择了零个节点。
您的 XML 具有 默认命名空间 - 声明的命名空间没有前缀 - 此处:
<SecurityDS xmlns="http://tempuri.org/SecurityDS.xsd">
后代元素隐式继承祖先默认值,除非另有说明。要在使用 XmlDocument
class 时访问命名空间中的元素,您需要将前缀映射到命名空间 URI,然后在 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);