I have previously posted how to use PowerShell to change properties for a search result web part (Make sure your people search is fuzzified)
My colleague Tarjei (@tarjeieo) is working on some templating for a provisioning engine using CSOM and he needed to change some values in a web part file before loading it into a page. This involves both XML parsing with namespaces and making sure you don’t cripple the JSON object you are changing.
Here’s the code I ended up with to modify the SourceName property for a search result web part file, ignoring the namespaces in the xml file.
XElement doc = XElement.Load(@"D:\Temp\test.webpart");
var element = doc.XPathSelectElement(".//*[local-name() = 'property' and @name='DataProviderJSON']");
dynamic dp = JObject.Parse(element.Value);
dp.SourceName = "lala";
element.Value = JObject.FromObject(dp).ToString();
doc.Save(@"D:\Temp\test.webpart2");