0

xmlfile から 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);

すべての場合において、ゼロのノードが選択されます。

4

1 に答える 1

1

あなたの XML にはデフォルトの名前空間があります -名前空間は接頭辞なしで宣言されています - ここに:

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

子孫要素は、特に指定がない限り、祖先のデフォルトを暗黙的に継承します。クラスを使用するときに名前空間の要素にアクセスするXmlDocumentには、プレフィックスを名前空間 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);
于 2015-08-30T03:23:35.843 に答える