// Collection of code snippets by Arne Vajhøj // posted to eksperten.dk, usenet and other places (2002-now) using System; using System.Xml; namespace Feb2 { public class Program { public static void SetNS(XmlNode n, string prefix, string nsurl, bool recurse) { 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)); if(recurse) { SetNS(newelm.LastChild, prefix, nsurl, recurse); } } } } public static void Main(string[] args) { string s = @" tekst 323700 11 4 "; XmlDocument doc = new XmlDocument(); doc.LoadXml(s); doc.Save(Console.Out); SetNS(doc.DocumentElement, "ns1", "http://foobar", false); doc.Save(Console.Out); SetNS(doc.DocumentElement, "ns1", "http://foobar", true); doc.Save(Console.Out); } } }