How to remove all namespaces from XML with C#?

前端 未结 30 2667
悲哀的现实
悲哀的现实 2020-11-22 13:30

I am looking for the clean, elegant and smart solution to remove namespacees from all XML elements? How would function to do that look like?

Defined interface:

30条回答
  •  礼貌的吻别
    2020-11-22 13:47

    Here's are Regex Replace one liner:

    public static string RemoveNamespaces(this string xml)
    {
        return Regex.Replace(xml, "((?<=<|<\\/)|(?<= ))[A-Za-z0-9]+:| xmlns(:[A-Za-z0-9]+)?=\".*?\"", "");
    }
    

    Here's a sample: https://regex101.com/r/fopydN/6

    Warning:there might be edge cases!

提交回复
热议问题