I can see a way of searching for an element within XML by just going:
if(doc.SelectSingleNode("//mynode")==null)
But what I'm more interested in, is finding an element that matches the part of the name. Something like:
doc.SelectSingleNode ...that contains "table" in it.
So if I had a node called "AlinasTable", I want it to find that. Why it matters is because my node can inconsistently contain anything that comes before "table", like "JohnsTable" - in which case I'd want that to be returned. So something more generic.
Cheers.
You can use the contains function, as in the following XPath expression:
doc.SelectSingleNode("//*[contains(name(), 'Table')]")
Related
I got the id for capturing.
//button[contains(text(),'Delete')][1]
//button[#id='deletebtn']
but its have 10 duplicate values.can't identify unique thing for capture the element.Please Help me to resolve
As you haven't shared full HTML I am assuming you want use the first locator of 10 matches.
You can use
//(button[contains(text(),'Delete')])[1]
instead of
//button[contains(text(),'Delete')][1]
In case, suppose you want to use an another element then change the match number. Like below,
//(button[contains(text(),'Delete')])[3]
or
//(button[contains(text(),'Delete')])[4]
Note: Selenium by default picks the first element if there are more than one match.
Always check your xPath in chrome console to make sure it is unique.
Press F12 in Chrome.
Go to elements section
Search ( CTRL + F)
Place the xpath and see, if your desired element is getting highlighted with 1/1 matching node. This means, your xPath is unique.
You are using the wrong syntax to write the index based xpath.
Correct syntax for index based XPath-
(//button[contains(text(),'Delete')])[10]
PS: You can verify in SelectorsHub if your xpath is correct or not.
Please have a look at the following lines of XML codes. My goal is to extract the values in the interactor element:
<HPRD3r xmlns="org:hprd:dtd:hprd3r">
<interactions>
<entrySet xsi:schemaLocation="net:sf:psidev:mi http://psidev.sourceforge.net/mi/rel25/src/MIF25.xsd">
<interactionList>
<interactor>
For simplicity, let's assume interactions is a direct child of root.
Set the namespace as follows,
XNamespace ns = "org:hprd:dtd:hprd3r";
The following always returns null although "entrySet" is present:
root.Element(ns+"interactions").Element(ns+"entrySet");
On the other hand,
root.Descendants(ns+"interactor");
does not return null but gives a count of zero even if there are more than one interactor elements in the file.
Seems like the problem is the attribute xsi:schemaLocation in entrySet. Would someone explain to me please the reasons behind the problems above and how to fix them.
Thanks
I'm using XDocument and I need to parse my XML file to retrieve all attribute with the same name event if its node's name is different from the other.
For example, for this XML :
<document>
<person name='jame'/>
<animals>
<dog name='robert'/>
</animals>
</document>
I want to retrieve all attributes named 'name'.
Can I do that with one request XPath or do I need to parse every node to find thos attributes ?
Thanks for your help !
The XPath expression
//#name
will select all attributes called name, regardless of where they appear.
By the way, 'parsing' is something that happens to the XML document before XPath ever enters the picture. So when you say "do I need to parse every node", I think this isn't really what you mean. The entire document is typically already parsed before you run an XPath query. However, I'm not sure what you do mean instead of 'parse'. Probably something like "do I need to visit every element" to find those attributes? In which case the answer is no, unless in some vague implementation-dependent sense that doesn't make any difference to you.
I am trying to select all nodes with attribute equal to something, I got the error in title.
My Xpath string looks like //#[id=****], anyone know what's wrong?
Your XPath expression probably should be:
//*[#id='something']
Which means match all elements whose id attributes are equal to something, anywhere in the document.
EDIT: If you want the id attribute nodes themselves and not their parent elements, you can use:
//*[#id='something']/#id
Or even better, as #Dimitre Novatchev suggested:
//#id[. = 'something']
I am trying to select all nodes with
attribute equal to something, I got
the error in title.
My Xpath string looks like
//#[id=****], anyone know what's
wrong?
A lot of issues with this expression:
.1. //#[some-condition] The predicate can only be applied to selected nodes, but //# doesn't select any node. # is an abbreviation for attribute:: and this is an unfinished node-test. It is missing the node-type or node-name here.
What would be correct is: //#*[some-condition] or //#attrName[some-condition]
.2. id=**** is syntactically invalid, unless ** is a valid XPath expression itself. My guess is that you want to get all attributes with value equal to some known, literal value. In any such case the syntax to use is id='someLiteral -- do note the single quotes (they can also be double quotes) surrounding the literal value.
Solution:
//*[#id='something']
This selects all elements in the XML document that have attribute id with value 'something'.
//#id[. = 'something']
This selects all attributes named id in the XML document, whose value is 'something'.
//#*[. = 'something']
This selects all attributes in the XML document (regardless of their name), whose value is 'something'.
I have an XML document that I load in and try to search with XPath. The root node in this file is <t:Transmission xmlns:t='urn:InboundShipment'> and the file end is properly closed with </t:Transmission>.
My problem is that I cannot walk the tree without using a descendant axis. In other words, I can do: SelectSingleNode("//TransactionHeader[SHIPPERSTATE='CA']") and get a node in return. But I cannot do what should be the equivalent: SelectSingleNode("/Transmission/TransmissionBody/Transaction/TransactionHeader[SHIPPERSTATE='CA']")
If I remove the t: I can do an XPath search on /Transmission and get the whole file. With the t: in there I just get null. Or if I try SelectSingleNode("t:Transmission") I get an error with my XPath statement.
I generally do not need to query the root element, so I should be able to make do with just using the descendant axis for my searches. But the XML looks valid to me and so I'd like to know how to address this. Plus I don't want to ask the client to remove "t:" just because I don't know how to deal with it.
The "t:" is a namespace prefix, which is bound to the namespace 'urn:InboundShipment.' In order to properly handle it, you have to tell c# what the prefix is bound to. This page should explain how to use System.Xml.XmlNamespaceManager to handle the namespace.
Edit: See this answer, as well.