Skip to content

Instantly share code, notes, and snippets.

@domenu
Created November 5, 2014 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domenu/b932f48bac76ec5b95b3 to your computer and use it in GitHub Desktop.
Save domenu/b932f48bac76ec5b95b3 to your computer and use it in GitHub Desktop.
Change namespace in XML
XNamespace ns = "http://mycompany/myproduct/v2";
foreach (XElement el in document.Root.DescendantsAndSelf())
{
el.Name = ns.GetName(el.Name.LocalName);
List<XAttribute> atList = el.Attributes().ToList();
el.Attributes().Remove();
foreach (XAttribute at in atList)
{
el.Add(new XAttribute(ns.GetName(at.Name.LocalName), at.Value));
}
}
document.Root.Add(new XAttribute(XNamespace.Xmlns + "ns", ns));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment