0

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?

Community
  • 1
  • 1
Devin
  • 1,014
  • 1
  • 9
  • 28
  • 4
    Just guessing but wouldn't AssemblyName also be in the namespace denoted by x in this XPath: //x:PropertyGroup//AssemblyName. I.e. (without knowing the data) I bet it should be //x:PropertyGroup//x:AssemblyName – dkackman May 07 '12 at 22:44
  • yeah, that was it. Also, I can't just do `node.Value`, I need to do `node.FirstChild.Value`. Thanks! – Devin May 07 '12 at 22:58

1 Answers1

0

Seems this is solved by comment:

Just guessing but wouldn't AssemblyName also be in the namespace denoted by x in this XPath: //x:PropertyGroup//AssemblyName. I.e. (without knowing the data) I bet it should be //x:PropertyGroup//x:AssemblyName – dkackman

robrich
  • 13,017
  • 7
  • 36
  • 63