简体   繁体   中英

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.

尝试将“ and”替换为“] [”,如下所示:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM