Tip: Remove namespaces using XDocument and LINQ

sep 30, 2011 22:38:56   //   Geert Van Huychem   //   Freebies   //   0 reacties

Ever crossed the point where your XPath expressions just didn't work, no matter what you tried. Chances are big that's because there are namespaces used in the xml

Below you'll find a simple way to remove them, using LINQ. Pay attention to the highlighted line. If you don't do this, all attributes will be removed.

                                                    using System.Linq;
                                                    using System.Xml.Linq;
            
                                                    namespace iFrameWorx.Core.UnitTests.Extensions
                                                    {
                                                        public static class XElementExtensions
                                                        {
                                                            public static XElement RemoveNamespaces(this XElement root)
                                                            {
                                                                var res = new XElement(
                                                                    root.Name.LocalName,
                                                                    root.HasElements ?
                                                                                         root.Elements().Select(RemoveNamespaces) :
                                                                                                                                      (object)root.Value
                                                                    );
            
                                                                res.ReplaceAttributes(
                                                                    root.Attributes().Where(attr => (!attr.IsNamespaceDeclaration)));
            
                                                                return res;
                                                            }
                                                        }
                                                    }
                                                
    
                                                using System.Linq;
                                                using System.Xml.Linq;
            
                                                using NUnit.Framework;
            
                                                namespace iFrameWorx.Core.UnitTests.Extensions
                                                {
                                                    [TestFixture]
                                                    public class RemoveNamespacesUnitTest
                                                    {
                                                        [Test]
                                                        public void RemoveNameSpaces()
                                                        {
                                                            var xDocumentOriginal = XDocument.Load("../../624081.xml", LoadOptions.None);
                                                            Assert.IsNotNull(xDocumentOriginal);
                                                            Assert.IsNotNull(xDocumentOriginal.Root);
            
                                                            Assert.IsTrue(xDocumentOriginal.Root.Attributes().Where(attr => attr.IsNamespaceDeclaration).Count() > 0);
            
                                                            var xDocumentNew = XDocument.Parse(xDocumentOriginal.Root.RemoveNamespaces().ToString(), LoadOptions.None);
                                                            Assert.IsNotNull(xDocumentNew);
                                                            Assert.IsNotNull(xDocumentNew.Root);
            
                                                            Assert.IsTrue(xDocumentNew.Root.Attributes().Where(attr => attr.IsNamespaceDeclaration).Count() == 0);
                                                        }
                                                    }
                                                }
                                                

Reageer.

Enkele items ontbreken of zijn fout ingevuld.