Using C # to search for nodes in .csproj files

I'm having trouble trying to select a specific node from a .csproj file, which I read as an XDocument.

XDocument xmldoc = XDocument.Load("The full path of the .csproj file"); 

This loads the .csproj file into an XDocument without problems. I tried Descendants , Elements , etc., to try to get TheNodeIWant and its value, but I can not understand why I am not getting any results.

 <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <TheNodeIWant>The String I Want </TheNodeIWant> </PropertyGroup> <PropertyGroup> ....... </PropertyGroup> </Project> 

How can I select TheNodeIWant and get its value?

+4
source share
3 answers

see fooobar.com/questions/173285 / ...

 XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(@"c:\test.txt"); XmlNamespaceManager ns = new XmlNamespaceManager(xmldoc.NameTable); ns.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003"); XmlNode node = xmldoc.SelectSingleNode("//msbld:TheNodeIWant", ns); if (node != null) { MessageBox.Show(node.InnerText); } 
+6
source

You can use the MSBuild Project class to better use csproj from code.

+4
source

I suggest using Xpath to navigate inside your csproj file.

+1
source

All Articles