// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Xml; namespace E { public class Program { public static void SetNS(XmlNode n, string prefix, string nsurl) { if(n.NodeType == XmlNodeType.Element) { XmlElement oldelm = (XmlElement)n; XmlElement newelm = oldelm.OwnerDocument.CreateElement(prefix, oldelm.LocalName, nsurl); oldelm.ParentNode.ReplaceChild(newelm, oldelm); while(oldelm.HasChildNodes) { newelm.AppendChild(oldelm.RemoveChild(oldelm.FirstChild)); SetNS(newelm.LastChild, prefix, nsurl); } } } public static void Main(string[] args) { string s = @" A B "; XmlDocument doc = new XmlDocument(); doc.LoadXml(s); doc.Save(Console.Out); XmlNode target = doc.SelectSingleNode("//foo/bar[1]"); SetNS(target, "gml", "http://gml"); doc.Save(Console.Out); } } }