How to remove all namespaces from XML with C#?

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

    my answer, string-manipulation-based,
    lite-most code,

    public static string hilangkanNamespace(string instrXML)
        {
            char chrOpeningTag = '<';
            char chrClosingTag = '>';
            char chrSpasi = ' ';
            int intStartIndex = 0;
            do
            {
                int intIndexKu = instrXML.IndexOf(chrOpeningTag, intStartIndex);
                if (intIndexKu < 0)
                    break; //kalau dah ga ketemu keluar
                int intStart = instrXML.IndexOfAny(new char[] { chrSpasi, chrClosingTag }, intIndexKu + 1); //mana yang ketemu duluan
                if (intStart < 0)
                    break; //kalau dah ga ketemu keluar
                int intStop = instrXML.IndexOf(chrClosingTag, intStart);
                if (intStop < 0)
                    break; //kalau dah ga ketemu keluar
                else
                    intStop--; //exclude si closingTag
                int intLengthToStrip = intStop - intStart + 1;
                instrXML = instrXML.Remove(intStart, intLengthToStrip);
                intStartIndex = intStart;
            } while (true);
    
            return instrXML;
        }
    

提交回复
热议问题