2011-06-17 15 views
0

これは、XML文書の一部です:XMLノードから複数の値を取得(XPathの)

<directory level="Beginner"> 
    <cel row="0" column="0" visible="no">2</cel> 
</directory> 

がどのように私は正確に文字列でこれらの3つの値を置くことができますか?

文字列がnullを返すたびに次のXPathとしてXPRESSION:私は、文字列からその一部を残せばもうnullではないので、

string xpath = "//directory[@level='" + directoryLevel+ "'] /cel[@row='" + amountOfRows + "' and @column='" + amountOfColumns+ "']"; 

エラーは、どこか「と」(第二属性)の周りにする必要があります。

どのようにして正確に列の値を取得できますか?このエラーの原因は何ですか?

よろしくお願いいたします。

+1

結果の文字列は正確には何ですか? –

答えて

0

そうのように「[]」「と」とを交換してみてください:

string xpath = "//directory[@level='" + directoryLevel+ "']/cel[@row='" + amountOfRows + "'][@column='" + amountOfColumns+ "']"; 
0

ので、私は問題が何であるかわからない有効なXPathのように見えます。私は、XMLと静的なテキストと少し周りをテストされ、正常に動作し、次のXPathステートメントを構築することができました:

//directory[@level='Beginner']/cel[(@row='0') and (@column='0')] 

は問題があなたの変数とないのXPathに関連していませんか?あなたは絶対に "amountOfRows"と "amountOfColumns"があなたが期待する値を持っていると思いますか?

+0

はい、行はforループにあり、同様に列も同じです。初心者と2回0デバッグ、まだ文字列がnullと言う。 – Dajing

0

あなたの問題は他の場所のようです。

XmlDocument document = new XmlDocument(); 
document.LoadXml("<directory level=\"Beginner\"><cel row=\"0\" column=\"0\" visible=\"no\">2</cel></directory>"); 

string directoryLevel = "Beginner"; 
string amountOfRows = "0"; 
string amountOfColumns = "0"; 

string xpath = "//directory[@level='" + directoryLevel+ "']/cel[@row='" + amountOfRows + "' and @column='" + amountOfColumns+ "']"; 
XmlNode node = document.SelectSingleNode(xpath); 

Console.WriteLine(node.OuterXml); 

出力した:

<cel row="0" column="0" visible="no">2</cel> 

結論:私は、次のコードを書いた予想通りXPath式が動作します。

関連する問題