If you want to use LINQ to XML for the purpose, then your code can look like this:
var input = @"d:\temp\in.xml";
var output = @"d:\temp\out.xml";
try
{
// load original file
var xmlDocument = XDocument.Load(input);
// don't forget to take the namespace, otherwise LINQ will not find the elements
XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
// finder helper
Func<string, IEnumerable<XElement>> getElements = elementName =>
// find all elements of type ItemGroup
// that contain elements of the given type (here ClInclude or ClCompile)
xmlDocument.Root.Elements(ns + "ItemGroup").Elements(ns + elementName);
// find elements: ClInclude
var clInputs = getElements("ClInclude");
foreach (var clInput in clInputs)
{
// fetch attribute
var inputAttribute = clInput.Attributes("Include");
// take first and modify it
inputAttribute.ElementAt(0).Value = "some new value";
}
// find elements: ClCompile
var clCompiles = getElements("ClCompile");
foreach (var clCompile in clCompiles)
{
// fetch attribute
var compileAttribute = clCompile.Attributes("Include");
// take first and modify it
compileAttribute.ElementAt(0).Value = "some other value";
}
// save modified file under new name
xmlDocument.Save(output);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
The modified sections in the new output looks like this:
<ItemGroup>
<ClCompile Include="some other value" />
<ClCompile Include="some other value" />
...
<ClCompile Include="some other value" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="some new value" />
...
<ClInclude Include="some new value" />
</ItemGroup>
More information on LINQ to XML can be found at MSDN.