CSharp · Console
using System;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
public class Program
{
public static void Main()
{
var xaml = @"<StackPanel>
<TextBlock Text='not example'/>
<!-- Some other stuff... -->
<GridViewColumn Header='example'>
<!-- Some other stuff... -->
</GridViewColumn>
</StackPanel>";
var templateXaml = @"<DataTemplate>
<TextBlock Text='example'/>
</DataTemplate>";
var doc = XDocument.Parse(xaml);
var template = XElement.Parse(templateXaml);
//XNamespace d = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
foreach (var attr in doc.Descendants().Attributes("Text").Where(o => o.Value != "example"))
{
attr.Value = "example";
}
foreach (var element in doc.Descendants().Where(o => o.Attribute("Header") != null))
{
element.DescendantNodes().Remove();
element.Add(new XElement(element.Name.LocalName + ".HeaderTemplate", template));
}
Console.WriteLine(doc.ToString());
}
}