I'm writing a utility that will change the assembly name of a csproj file. I know the csproj is essentially XML so XPath should work. In fact, I can get it to work for certain information. Here is the code I have:
var xmlDoc = new XmlDocument();
xmlDoc.Load(file);
XmlNamespaceManager mgr = new XmlNamespaceManager(xmlDoc.NameTable);
mgr.AddNamespace("x", xmlDoc.DocumentElement.NamespaceURI);
XmlNode node = xmlDoc.SelectSingleNode("//x:PropertyGroup//AssemblyName", mgr);
node.Value = newValue;
xmlDoc.Save(file);
However, node
is null. I've also tried using "//x:PropertyGroup[1]//AssemblyName"
to no avail. If I just try and find "//x:PropertyGroup"
it will work fine, so I'm assuming my issue is that not every PropertyGroup Node has an AssemblyName node.
I'm using a NamespaceManager per the suggestion in this thread and I've been able to retrieve the AssemblyName value via XLinq as suggested here, but I need to update the value, not just read it.
What am I missing?