For modifying the csproject file of mvc project, I used the code snippet like below, but failed to achieve it. could you please help me?
var newReference = @"System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL";
var profiles = Directory.GetFiles(path, "*.csproj", SearchOption.AllDirectories);
foreach (var profile in profiles)
{
var doc = XDocument.Load(profile);
var parentElement = doc.Descendants("ItemGroup").FirstOrDefault();
if (parentElement != null)
{
var newNode = new XElement("Reference", new XAttribute("Include", newReference));
parentElement.Add(newNode);
}
doc.Save(profile);
}
The “naive” answer is that you have to include the xml’s namespace
This will do what you try to achieve (adding a new reference in the first ItemGroup Element), but if you look at a csproj file, you’ll see that there’s not only one ItemGroup “group” !