It has taken a while, but I have finally been able to modify an XML document based on user input for the namespace and node name:
string nodeName = "DefinitionName"; // this is really provided by the user
string namespace = "http://schemas.datacontract.org/2004/07/Xxx.Session"; // also user-provided
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(taskResolved.XmlPathAndFileName);
XmlElement rootElement = xmlDocument.DocumentElement;
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
namespaceManager.AddNamespace("snuh", namespace); // hard-coded prefix, grrr...
XmlNodeList xmlNodes;
xmlNodes = rootElement.SelectNodes("//snuh:" + nodeName, namespaceManager);
I feel like I'm doing something wrong because I have to hard-code a prefix (snuh). I could try and choose a prefix, like snuh, that I can hope will never appear in a document, but that isn't foolproof. Another option is to use a GUID for a prefix, but this just seems like a hack work-around. Am I missing something? Is there a better way?
The top of the XML doc looks like this:
<?xml version="1.0" encoding="utf-8"?>
<SessionStateInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="1"
z:Type="Xxx.SessionStateInfo"
z:Assembly="Xxx.Common, Version=6.2.0.0, Culture=neutral, PublicKeyToken=null"
xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"
xmlns="http://schemas.datacontract.org/2004/07/Xxx.Session">
<CoaterNumber>25</CoaterNumber>
<DefinitionName z:Id="2">TwoLineMarkerDefinition</DefinitionName>
<EnableManualMode>true</EnableManualMode>