我想通过解析dotnet c#中的下面的xml来了解ItemGroup的名称。我尝试了各种选择,但无法得到它。基本上我想知道有ItemGroup,它可以更多。无论如何我们可以加载这个xml并获取ItemGroups的列表及其名称。
这里我希望ItemGroup名称为“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>
我试过如下
XmlDocument objXML = new XmlDocument();
objXML.Load(path);
然后开始得到孩子和所有孩子。
示例XML看起来像这样
<?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>
答案 0 :(得分:1)
使用LINQ to XML
XDocument objXML = new XDocument();
objXML.Load(path);
您的LINQ代码看起来像这样
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;
}
}
<强>参考强>
以下是一个示例应用程序。
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();
}
}
}
这是一款全新的C#控制台应用。我正在加载项目自己的.csproj文件。
我无法针对您的示例XML执行此代码。我怀疑这是因为它打破了http://schemas.microsoft.com/developer/msbuild/2003定义的模式。
答案 1 :(得分:0)
您可以像这样使用Microsoft.Build:
var pathToXml = @"<path>";
var nodes = ProjectRootElement.Open(pathToXml).AllChildren;
var itemGroupElements = nodes.OfType<ProjectItemGroupElement>();
然后抓住每个商品组的第一个孩子并获得其名称:
foreach (var itemGroup in itemGroupElements)
{
Console.WriteLine(itemGroup.FirstChild.ElementName);
}