0

I have the very basics of XPaths down, but am having some trouble in determining if the following is possible in C# code using an XPath (or if I need to move it out into other code, as I currently have done).

I have an XML Document that consists of the following structure:

 <xml>
     <parameters>
        <setParameter name="SomeName" value="SomeValue" />
     </parameters>
 </xml>

Where there are multiple set Parameter values. Now what I need to do is only retrieve those setParameter nodes that contain certain values for the name attribute. I may have a list of possible matches for these values, but they won't be full matches, they will be values the node's name attribute must contain.

For example in the structure code above, if I had a value of 'men' to match, it would come back with the node, as 'men' is contained in 'SomeName'

What is the shorthand to do this?

2
  • do you have an xpath example? Commented Feb 13, 2017 at 16:35
  • very simple at: "parameters/setParameter" Commented Feb 13, 2017 at 16:36

3 Answers 3

1

Retrieving the value of all attributes named value for all elements named setParameter having a name attribute's value containing men:

//setParameter[contains(@name, 'men')]/@value
  • //setParameter

Retrieves all nodes named setParameter (can be replaced with /xml/parameters/setParameter)

  • [...]

Checks an attribute for the current node selection

  • contains(@name, 'men')

Returns true if the name attribute's value contains men

  • /@value

Retrieves the value attribute's value.

Sign up to request clarification or add additional context in comments.

4 Comments

Is this case-insensitive?
@Polyfun No, it's case sensitive. See this answer for case-insensitiveness.
OP wanted a case-insensitive match.
@Polyfun I must be sleepy, but I don't see this requirement.
0

I don't think there is a way to match an attribute with a wildcard, but you could use the contains method, something like:

//parameters/setParameter[contains(@name, "stringexample")]

Comments

0

Depending on your XPath version this might work or might not:

//setParameter[matches(@name,"men", "i") or matches(@name,"else", "i")]

This should match <setParameter> with name attribute that contains "men" or something "else". It is case-insensitive

Try and let me know the result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.