简体   繁体   中英

Extracting values from xml using xpath based on a condition

I am using xpath to parse and get the attribute values from a xml file.

This is my xpath expression

./result/object/group[@mode = 'invalid']

The xpath generates

<group mode="invalid" name="3"/>
<group mode="invalid" name="4"/>

I would want to further, parse and get the details of the name, either like

name="3"
name="4"

or just

"3"
"4"

I am not sure of how to proceed further based on conditions.

You can get the "name" by using the GetAttribute method. For example in your case:

string name  = driver.FindElement(By.Xpath("./result/object/group[@mode = 'invalid']")).GetAttribute("name");

I managed to finish the operation using ./result/object/group[@mode = 'invalid']/@name to get the name values. I used the following to get the values of the name

XmlNodeList xmlNodeList = xmlDoc.SelectNodes("./result/object/group[@mode = \'invalid\']/@name");

    foreach (XmlAttribute node in xmlNodeList)
    {
        Console.WriteLine(node.Value);
    }

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