Its a .vbproj and looks like this
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>15a7ee82-9020-4fda-a7fb-85a61664692d</ProjectGuid>
all i want to get is the ProjectGuid but it does not work when a namespace is there...
Dim xmlDoc As New XmlDocument()
Dim filePath As String = Path.Combine(mDirectory, name + "\" + name + ".vbproj")
xmlDoc.Load(filePath)
Dim value As Object = xmlDoc.SelectNodes("/Project/PropertyGroup/ProjectGuid")
what can i do to fix this?
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):
I include only one namespace here, but you could have multiple. Then you can select from the document like this:
Note that all of the elements are in the specified namespace.
You need just to register this XML namespaces and associate with a prefix, to make the query work. Create and pass a namespace manager as second parameter when selecting the nodes:
Why not use the // to ignore the namespace:
// acts as wild card to follow through everything between the root and the next node name specified(i.e ProjectGuid)
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):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:One way is to use extensions + NameSpaceManager.
Code is in VB but is realy easy to translate to C#.
And to use it: