How to remove all namespaces from XML with C#?

前端 未结 30 2725
悲哀的现实
悲哀的现实 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:45

    I think this is shortest answer(but for constuctions like , you will have another discussion, I also have regex to convert "" to "" but it wasn't optimized, If someone ask me I will share it. So, my solution is:

        public string RemoveAllNamespaces(string xmlDocument)
        {
            return Regex.Replace(xmlDocument, @"\sxmlns(\u003A\w+)?\u003D\u0022.+\u0022", " ");
        }
    

提交回复
热议问题