开发者

Retrieving several values from a xml node (xpath)

This is part of a xml document:

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

How can i exactly put those 3 values in a string?

Every time my string returns null. Xpath xpression as following:

string xpath = "//directory[@level='" + directoryLevel+ "'] /cel开发者_如何学运维[@row='" + amountOfRows + "' and @column='" + amountOfColumns+ "']";

The error has to be somewhere around the "and" (2nd attribute), because if i leave that part out the string is not null anymore.

How exactly can i get the value of column into my string and what causes this error?

Best Regards.


Try replacing the "and" with "][" like so:

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


It appears to be valid xpath so I am not sure what the problem is. I tested around a little with the xml and static text and was able to build the following xpath statement which works fine:

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

Could the problem be related to your variables and not xpath? Are you absolutely sure "amountOfRows" and "amountOfColumns" have the values you expect?


It looks like your problem is somewhere else. I wrote the following code:

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);

The output was:

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

Conclusion: the xpath expression works as expected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜