public ActionResult Index()
{ XDocument doc = XDocument.Load(@"G:\users\kim.gao\documents\visual studio 2015\Projects\test\test\test.csproj");
ContainFileToCsproj(@"T4\AccountViewModels.cs", "", "", doc);
doc.Save(@"G:\users\kim.gao\documents\visual studio 2015\Projects\test\test\test.csproj");
return View();
}
public static void ContainFileToCsproj(string fileName, string solutionDir, string csprojName, XDocument doc)
{
string path = @"G:\users\kim.gao\documents\visual studio 2015\Projects\test\test\test.csproj";
XElement root = doc.Root;
string xmlns = "{" + root.Attribute("xmlns").Value + "}";
IEnumerable<XElement> compileList = root.Elements(xmlns + "ItemGroup").Elements(xmlns + "Compile");
XElement itemGroup = compileList.FirstOrDefault().Parent;
if (compileList.Where(u => u.Attribute("Include").Value == fileName).Count() > )
{
compileList.Where(u => u.Attribute("Include").Value == fileName).Remove();
} XElement compile = new XElement(xmlns + "Compile");
compile.SetAttributeValue("Include", fileName);
itemGroup.AddFirst(compile);
}