XPath select node with namespace
I'd probably be inclined to go with Bartek's* namespace solution, but a general xpath solution is:
//*[local-name()='ProjectGuid']
**since Bartek's answer has disappeared, I recommend Teun's (which is actually more thorough)*
The best way to do things like this (IMHO) is to create a namespace manager. This can be used calling SelectNodes to indicate which namespace URLs are connected to which prefixes. I normally set up a static property that returns an adequate instance like this (it's C#, you'll have to translate):
private static XmlNamespaceManager _nsMgr;public static XmlNamespaceManager NsMgr{ get { if (_nsMgr == null) { _nsMgr = new XmlNamespaceManager(new NameTable()); _nsMgr.AddNamespace("msb", "http://schemas.microsoft.com/developer/msbuild/2003"); } return _nsMgr; }}
I include only one namespace here, but you could have multiple. Then you can select from the document like this:
Dim value As Object = xmlDoc.SelectNodes("/msb:Project/msb:PropertyGroup/msb:ProjectGuid", NsMgr)
Note that all of the elements are in the specified namespace.
This problem has been here several times already.
Either you work with namespace-agnostic XPath expressions (not recommended for its clumsiness and the potential for false positive matches - <msb:ProjectGuid>
and <foo:ProjectGuid>
are the same for this expression):
//*[local-name() = 'ProjectGuid']
or you do the right thing and use a XmlNamespaceManager
to register the namespace URI so you can include a namespace prefix in your XPath:
Dim xmlDoc As New XmlDocument()xmlDoc.Load(Path.Combine(mDirectory, name, name + ".vbproj"))Dim nsmgr As New XmlNamespaceManager(xmlDoc.NameTable)nsmgr.AddNamespace("msb", "http://schemas.microsoft.com/developer/msbuild/2003")Dim xpath As String = "/msb:Project/msb:PropertyGroup/msb:ProjectGuid"Dim value As Object = xmlDoc.SelectNodes(xpath, nsmgr)