Remove NameSpace In XML Document


 public static string RemoveNamespace(XmlDocument xdoc)
        {
            // Regex to search for either xmlns:nsN="..." or xmlns="..."
            const string pattern = @"xmlns:?(\w+)?=""[\w:/.]+""";
            string xml = xdoc.OuterXml;

            Regex replace = new Regex(pattern);
            // Replace all occurances of the namespace declaration
            string temp = replace.Replace(xml, String.Empty);
            foreach (Match match in replace.Matches(xml))
            {
                // Loop through each Match in the Regex, this gives us the namespace aliases found
                if (match.Success && match.Groups.Count > 1)
                {
                    if (!String.IsNullOrEmpty(match.Groups[1].Value))
                    {
                        Regex alias = new Regex(@"\b" + match.Groups[1].Value + ":");
                        // Replace all occurances of the
                        temp = alias.Replace(temp, String.Empty);
                    }
                }
            }
            return temp;
        }
Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment