Click here to Skip to main content
15,742,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to extract all conditional compilation constsants from a given project file. The file has several PropertyGroup elements like this:
XML
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
	<DebugSymbols>true</DebugSymbols>
	<DebugType>full</DebugType>
	<Optimize>false</Optimize>
	<OutputPath>bin\Debug\</OutputPath>
	<DefineConstants>DEBUG;TRACE</DefineConstants>
	<ErrorReport>prompt</ErrorReport>
	<WarningLevel>4</WarningLevel>
</PropertyGroup>

My first attempts at Linq to Sql all gave me all nodes, or none. What queries can I run to:

(a) get all DefineConstants elements, but include their parent element in the query, because I need the Condition attribute, or
(b) get all PropertyGroup elements that have DefineConstants children and include this children.

I am trying to build a dictionary of conditional compilation constants, so next time you come across a constant like DEVIL_FROG_POTATO_GREEN, you can just pop open my dictionary window and read, or hopefully, write what it does for others.
Posted

For Linq to xml you need to add following name space
1. System.Xml
2. System.Xml.Linq

we have number of option to query desired node from the xml ( paranet node and child node )
Please check on following links for the same

https://msdn.microsoft.com/en-us/library/bb943906.aspx[^]

http://www.dotnetcurry.com/showarticle.aspx?ID=564[^]


Parse XML Documents by XMLDocument and XDocument[^]



Parse XML Documents by XMLDocument and XDocument[^]


LINQ to XML[^]

http://codesamplez.com/programming/linq-to-xml-tutorial[^]
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace LinqToXml
{
class Program
{
static void Main(string[] args)
{
string proj = @"<?xml version=""1.0""?>
<Project>
<PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
</Project>";


XElement projElement = XElement.Parse(proj);
IEnumerable<XElement> constants = from x in projElement.Elements("PropertyGroup")
where x.Descendants("DefineConstants").Count() > 0
select x;
Console.WriteLine(string.Join(", ", constants.Select(x => x)));


}
}
}
 
Share this answer
 
v2
Comments
Brady Kelly 27-Jan-15 20:34pm    
Thanks John, this is nearly exactly what I need, but it only seems to work using your XML literal. If I try XElement.Load, or File.ReadAllText and XElement.Parse, on the real project file I want to query, I get zero results. I am checking further now.
John C Rayan 28-Jan-15 1:46am    
Can you give us a sample code of what you do
John C Rayan 28-Jan-15 4:48am    
I suspect your xml structure... Can you give us the xml
Brady Kelly 28-Jan-15 5:12am    
The XML is directly from a project (.csproj) file.
John C Rayan 28-Jan-15 5:41am    
Can you post the code that load and process the xml

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900