views:

28

answers:

1

Hi,

I'd like to know how to search for a string within an xml document. The object type is System.Xml.XmlNode.XmlDocument. The string can be anything with the document. I.e. attribute or element.

I tried

Select-Xml -Xml $xml -XPath "./Test"

but got no results

+2  A: 

The pattern you are trying to use selects root nodes named Test.

You could use the pattern (//text()|//@*)[contains(string(), "test")], that selects the attributes that contain the string test or the text nodes that contain it (i.e. not the elements).

But you want to select the elements, right? Using (//*|//@*)[contains(., "test")] does that, but it selects elements that contain the string test, even if it is through some child element, which is not what is wanted either.

So I guess you'll have to use something like (//*[contains(text(), "test")]|//@*[contains(., "test")]), which gives you what you want, but is not very pretty.

svick
Just ran into another issue. How can I include this xpath query to also search through comments in the xml doc?
Henno