Modifying an XML node with user-provided namespace and node value

stormcraft

New Member
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:\[code\]string nodeName = "DefinitionName"; // this is really provided by the userstring namespace = "http://schemas.datacontract.org/2004/07/Xxx.Session"; // also user-providedXmlDocument 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);\[/code\]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:\[code\]<?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>\[/code\]
 
Back
Top