Example #1
0
        /// <summary>
        /// Removes any XML namespaces from a <see cref="T:System.String" /> representing an XML fragment
        /// </summary>
        /// <param name="value"><see cref="T:System.String" /> representing an XML fragment</param>
        /// <returns><see cref="T:System.String" /> with namespaces removed</returns>
        public static String RemoveNameSpaces(this String value)
        {
            if (!String.IsNullOrEmpty(value))
            {
                using (StringWriter sw = new StringWriter())
                {
                    using (XmlNoNamespaceWriter xw = new XmlNoNamespaceWriter(sw, new XmlWriterSettings()
                    {
                        ConformanceLevel = ConformanceLevel.Fragment,
                        OmitXmlDeclaration = true,
                        Indent = false
                    }))
                    {
                        using (StringReader sr = new StringReader(value))
                        {
                            using (XmlReader xr = XmlReader.Create(sr, new XmlReaderSettings()
                            {
                                ConformanceLevel = ConformanceLevel.Fragment,
                                ValidationType = ValidationType.None
                            }))
                            {
                                while (xr.Read())
                                {
                                    while (xr.NodeType == XmlNodeType.Element ||
                                           xr.NodeType == XmlNodeType.Comment ||
                                           xr.NodeType == XmlNodeType.Whitespace ||
                                           xr.NodeType == XmlNodeType.SignificantWhitespace ||
                                           xr.NodeType == XmlNodeType.ProcessingInstruction ||
                                           xr.NodeType == XmlNodeType.DocumentType)
                                    {
                                        xw.WriteNode(xr, true);
                                    }
                                }
                            }
                        }
                    }

                    return(sw.ToString());
                }
            }

            return(String.Empty);
        }
        /// <summary>
        /// Strip all namespaces from an existing <see cref="T:System.Xml.XmlDocument"/>
        /// </summary>
        /// <param name="document"><see cref="T:System.Xml.XmlDocument"/></param>
        /// <returns>XML as <see cref="T:System.String"/> with namespaces removed.</returns>
        public static String RemoveNameSpaces(this XmlDocument document)
        {
            if (document != null)
            {
                using (StringWriter sw = new StringWriter())
                {
                    using (XmlNoNamespaceWriter xw = new XmlNoNamespaceWriter(sw, new XmlWriterSettings()
                    {
                        ConformanceLevel = ConformanceLevel.Auto,
                        OmitXmlDeclaration = true,
                        Indent = false
                    }))
                    {
                        document.Save(xw);
                    }

                    return(sw.ToString());
                }
            }

            return(String.Empty);
        }