简体   繁体   中英

Getting ItemGroup collection and their Items from MSBuild xml

I want to know the name of the ItemGroup by parsing the below xml in dotnet c#. I tried various options but couldn't get it. Basically i want to know that there are ItemGroup , it can be more. Is there anyway we can load this xml and get the ItemGroups's list and their names.

Here i want the ItemGroup name as "cssfile_individual" , cssfile_individual2

    <Project xmlns="http://schemas.microsoft.com/developer/MsBuild/2003">

      <UsingTask TaskName="CssCompressorTask" AssemblyFile="Yahoo.Yui.Compressor.Build.MsBuild.dll" />
      <UsingTask TaskName="JavaScriptCompressorTask" AssemblyFile="Yahoo.Yui.Compressor.Build.MsBuild.dll" />

      <PropertyGroup>

      </PropertyGroup>

        <Target Name="Minify">

        <ItemGroup>
          <cssfile_individual Include="test1.css"/>
          <cssfile_individual Include="test2.css"/>
          <cssfile_individual Include="test3.css"/>
          <cssfile_individual Include="test3.css"/>      
        </ItemGroup>

 <ItemGroup>
      <cssfile_individual2 Include="test1.css"/>
      <cssfile_individual2 Include="test2.css"/>
      <cssfile_individual2 Include="test3.css"/>
      <cssfile_individual2 Include="test3.css"/>      
    </ItemGroup>

        </Target>
    </Project>

I tried as below

 XmlDocument objXML = new XmlDocument();
             objXML.Load(path);

and then started getting childs and all.

Sample XML looks like this

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/MsBuild/2003">
  <UsingTask TaskName="CompressorTask"
             AssemblyFile="D:\JsCssCompressor\JsCssCompressor\bin\Yahoo.Yui.Compressor.Build.MsBuild.dll" />

  <Target Name="MyTaskTarget">
    <ItemGroup>
      <JavaScriptFiles Include="C:\Work\Purchase_Flow\eBizSol_App\Release\WebSites\Websites\McAfee.Consumer.Website\UIDesign\LegacySite\Scripts\FlexDashboard\AddDevice.js"/>
    </ItemGroup>
    <CompressorTask
        JavaScriptCompressionType="YuiStockCompression"
        JavaScriptFiles="@(JavaScriptFiles)"
        ObfuscateJavaScript="True"
        PreserveAllSemicolons="False"
        DisableOptimizations="Nope"
        EncodingType="Default"
        DeleteJavaScriptFiles="false"
        LineBreakPosition="-1"
        JavaScriptOutputFile="C:\Work\Purchase_Flow\eBizSol_App\Release\WebSites\Websites\McAfee.Consumer.Website\UIDesign\LegacySite\Scripts\FlexDashboard\MAA2.0.js"
        LoggingType="ALittleBit"
        ThreadCulture="en-us"
        IsEvalIgnored="false"
            />
  </Target>
</Project>

Use LINQ to XML

XDocument objXML = new XDocument();
objXML.Load(path);

Your LINQ code will look something like this

var ItemGroups = from IG in objXML.Descendants("ItemGroup")
                 select new {
                     Children = LG.Descendants()
                 };

//Print Results
string str = "";

foreach (var IG in ItemGroups){
  str += "Item Group Name: " + IG.Children[0].Name + Environment.NewLine;
  foreach (var IGValue in IG.Children){
    str += "  " + IGValue.Attribute("Include").Value + Environment.NewLine;
  }
}

References

Update

Here is a sample application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace testapp_xdocument_linq
{
    class Program
    {
        static void Main(string[] args)
        {

            XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
            XDocument X = XDocument.Load("C:\\Users\\Brian\\Documents\\Visual Studio 2012\\Projects\\testapp_xdocument_linq\\testapp_xdocument_linq\\testapp_xdocument_linq.csproj");

            var PropertyGroups = from PG in X.Descendants(ns + "PropertyGroup") select PG;

            //Print Results
            foreach (var element in PropertyGroups)
            {
                Console.WriteLine("First Descendant Name: " + element.Descendants().First().Name + Environment.NewLine);
            }

            Console.ReadLine();
        }
    }
}

This is a fresh C# console app. I am loading the project's own .csproj file.

I am not able to execute this code against your sample XML. I suspect this is because it breaks the schema defined by http://schemas.microsoft.com/developer/msbuild/2003 .

You can use Microsoft.Build like this:

var pathToXml = @"<path>";
var nodes = ProjectRootElement.Open(pathToXml).AllChildren;
var itemGroupElements = nodes.OfType<ProjectItemGroupElement>();

And then grab the first child of each Item Group and get its name:

foreach (var itemGroup in itemGroupElements)
{
    Console.WriteLine(itemGroup.FirstChild.ElementName);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM