why doesn't this xpath expression work ?

  • Thread starter Thread starter Coaster
  • Start date Start date
C

Coaster

I'm reading a csproj file trying to extract info via xpath and this first
one works fine

xmlNode =
xmlDoc.SelectSingleNode("/def:Project/def:PropertyGroup[1]/def:OutputType/text()",nsMgr);



but this one doesn't and the only change is the key name and that is
present. The xml is pasted below

xmlNode =
xmlDoc.SelectSingleNode("/def:Project/def:PropertyGroup[1]/def:AssemblyName/text()",
nsMgr);

I switch them around and run the second one first or even comment out the
first one and it still doesn't work (I mention that because I was reading
another post where someone was saying something about the node is removed
from the document and wouldn't be present for the second selectsinglenode,
not sure what thats about)

thanks

<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>

<Configuration Condition=" '$(Configuration)' == ''
">Debug</Configuration>

<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>

<ProductVersion>8.0.50727</ProductVersion>

<SchemaVersion>2.0</SchemaVersion>

<ProjectGuid>{106C49F4-4260-4A70-A362-84AF2DA95766}</ProjectGuid>

<OutputType>Exe</OutputType>

<AppDesignerFolder>Properties</AppDesignerFolder>

<RootNamespace>WelcomeEmail</RootNamespace>

<AssemblyName>WelcomeEmail</AssemblyName>

</PropertyGroup>



--------------------------------------------------------------------------------
 
Coaster said:
I'm reading a csproj file trying to extract info via xpath and this first
one works fine

xmlNode =
xmlDoc.SelectSingleNode("/def:Project/def:PropertyGroup[1]/def:OutputType/text()",nsMgr);



but this one doesn't and the only change is the key name and that is
present. The xml is pasted below

xmlNode =
xmlDoc.SelectSingleNode("/def:Project/def:PropertyGroup[1]/def:AssemblyName/text()",
nsMgr);

I switch them around and run the second one first or even comment out the
first one and it still doesn't work (I mention that because I was reading
another post where someone was saying something about the node is removed
from the document and wouldn't be present for the second selectsinglenode,
not sure what thats about)

thanks

<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>

<Configuration Condition=" '$(Configuration)' == ''
">Debug</Configuration>

<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>

<ProductVersion>8.0.50727</ProductVersion>

<SchemaVersion>2.0</SchemaVersion>

<ProjectGuid>{106C49F4-4260-4A70-A362-84AF2DA95766}</ProjectGuid>

<OutputType>Exe</OutputType>

<AppDesignerFolder>Properties</AppDesignerFolder>

<RootNamespace>WelcomeEmail</RootNamespace>

<AssemblyName>WelcomeEmail</AssemblyName>

</PropertyGroup>


Actually both don't work, my mistake. I had them working when I did a
SelectNodes call, I guess I'm not understanding the xpath very well
 
Ok, if none of them work, you probably forgot to add the namespace in your NamespaceManager

The top node defines this namespace

xmlns="http://schemas.microsoft.com/developer/msbuild/2003"

To query the nodes inside this namespace you need to specify the namespace in your XPath query. This is usually handled by a NamespaceManager which substitutes an alias you provide with the proper namespace in the query.

Before your first query try adding this line

nsMgr.AddNamespace("def", "http://schemas.microsoft.com/developer/msbuild/2003");

Both queries should then work, if not, please provide some more of your code for review.
 
Back
Top