'How to strip namespaces from Xml Document

I am having some trouble with removing namespaces from a document in order for a proprietary Parse() function to work properly. I am receiving an XML string that looks like this:

<r xmlns:p="p" xmlns:s="s" p:p="1">
<d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}">
    <r uid="{F72A4E1A-519B-42DA-A1E8-F7D10EA92C65}" p:before="r[@uid='{15E0854F-39D8-4890-8541-9C4102C0F944}']" s:id="{6AF25CA3-6AB7-4792-B582-5DE0DF6349D7}" s:ph="/body/main/tab_content/body_content_column"/>
    <r uid="{15E0854F-39D8-4890-8541-9C4102C0F944}" p:before="r[@uid='{7CCB5501-52DF-49F6-BE71-E7B0CA68EE3B}']" s:id="{FD61546D-1226-4292-85F2-656883507F45}" s:ph="/body/main/tab_content/body_content_column"/>
    <r uid="{7CCB5501-52DF-49F6-BE71-E7B0CA68EE3B}" p:before="r[@uid='{17AC1E20-55CB-4EA1-9073-35D9E776C9A3}']" s:id="{1778C38C-071A-4219-A141-4686B095E361}" s:ph="/body/main/tab_content"/>
    <r uid="{17AC1E20-55CB-4EA1-9073-35D9E776C9A3}" p:before="r[@uid='{9B34F931-3C72-40D2-90B7-D4DCC81163D5}']" s:id="{C1CF5E12-1793-4F2C-B49C-00D30F0F501F}" s:ph="/body/hero/booking_widget"/>
    <r uid="{9B34F931-3C72-40D2-90B7-D4DCC81163D5}" p:before="r[@uid='{957B0932-A2B0-406F-A2D4-8C37E9F9D3A7}']" s:cac="0" s:cnd="" s:mvt="" s:ds="" s:id="{DF8E2887-0BCA-4C46-B2E4-02CDD3652892}" s:par="" s:ph="/body/main/tab_content/body_tile_column" s:vbd="0" s:vbdev="0" s:vbl="0" s:vbp="0" s:vbqs="0" s:vbu="0"/>
    <r uid="{957B0932-A2B0-406F-A2D4-8C37E9F9D3A7}" p:before="r[@uid='{B6EBC78C-5C85-4256-9F9D-3DF2F907DFA6}']" s:cac="0" s:cnd="" s:mvt="" s:ds="" s:id="{69FF80E2-6E79-4556-AC95-E409121B15C7}" s:par="MapSize=mini" s:ph="/body/main/tab_content/body_tile_column" s:vbd="0" s:vbdev="0" s:vbl="0" s:vbp="0" s:vbqs="0" s:vbu="0"/>
    <r uid="{B6EBC78C-5C85-4256-9F9D-3DF2F907DFA6}" p:before="r[@uid='{6700A64F-D8B7-44D3-8BE8-A162563ADB01}']" s:cac="0" s:cnd="" s:mvt="" s:ds="" s:id="{A6F5FC25-AB71-4C3B-B7FA-EF1F790DA86A}" s:par="" s:ph="/body/main/tab_content/body_tile_column" s:vbd="0" s:vbdev="0" s:vbl="0" s:vbp="0" s:vbqs="0" s:vbu="0"/>
    <r uid="{6700A64F-D8B7-44D3-8BE8-A162563ADB01}" p:before="r[@uid='{9863B72F-AE05-4D34-B271-E1AD6D67B946}']" s:id="{0430BDF5-8E53-4B12-A8A2-C098F667E575}" s:ph="/body/main/tab_content/print_share_toolbar"/>
    <r uid="{9863B72F-AE05-4D34-B271-E1AD6D67B946}" p:before="r[@uid='{166944E6-FCEA-4CE6-A5DC-EC317337046B}']" s:id="{FDF0D36C-1863-4DAA-BB68-555A2F3A270A}" s:ph="/body/main/tab_menu"/>
    <r uid="{166944E6-FCEA-4CE6-A5DC-EC317337046B}" p:before="r[@uid='{E98B1E29-46AF-46A3-89C0-441443E97BA0}']" s:id="{F94A415B-6922-43F2-9DEE-F012DE1497BA}" s:ph="/body/main"/>
    <r uid="{E98B1E29-46AF-46A3-89C0-441443E97BA0}" p:before="r[@uid='{B03EF589-3103-448A-8105-BFC33BA953D0}']" s:id="{EAFA90C7-136A-41C8-9AE9-8B1123E7E3C9}" s:ph="body"/>
    <r uid="{969938EE-BF4F-4A42-BA30-A7AE12183B08}">
        <p:d/>
    </r>
    <r uid="{AAA093DC-A63F-4538-A94B-ECBB509008F5}">
        <p:d/>
    </r>
    <r uid="{16A7A1FE-5FB4-4394-B053-58147F3B584D}">
        <p:d/>
    </r>
    <r uid="{4CD0B2F1-CAC8-4C03-B11E-56AE8A2589E1}" p:before="*[1=2]"/>
</d>
</r>

I am using an edit utility to view the above XML in HTML format that shows this same output as:

<r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}" l="{EAB1DFDD-9918-4158-862B-BC3FD02B00CC}">
    <r id="{FC207214-4E05-40E9-B3C5-49AE41B6A323}" ph="head" uid="{B03EF589-3103-448A-8105-BFC33BA953D0}"/>
    <r id="{EAFA90C7-136A-41C8-9AE9-8B1123E7E3C9}" ph="body" uid="{969938EE-BF4F-4A42-BA30-A7AE12183B08}"/>
    <r id="{9325E0F7-A4B5-46EC-B178-E7EF051FB1FE}" ph="hero" uid="{6C998C3F-C968-466A-AB3A-C9C12C78A389}"/>
    <r id="{7728075C-15DE-4DBE-9018-32794A1540F0}" ph="breadcrumb" uid="{593B65AF-BFA4-45F0-9EF0-12806CF54468}"/>
    <r id="{C1CF5E12-1793-4F2C-B49C-00D30F0F501F}" ph="booking_widget" uid="{AAA093DC-A63F-4538-A94B-ECBB509008F5}"/>
    <r id="{2BA579A5-06C8-41D7-B170-EA745D4CDE8E}" ph="image_rotator" uid="{CC96DC9A-60EB-4C68-8F32-06838EB69818}"/>
    <r uid="{F8E91E5B-1C55-42F6-96C2-52B1FE86ECEA}" id="{C8B42985-3D99-4E08-951C-2C3A98A737B4}" ph="main"/>
    <r uid="{209FDA28-CA6D-4306-9587-294E35849620}" id="{0430BDF5-8E53-4B12-A8A2-C098F667E575}" ph="print_share_toolbar"/>
    <r id="{F94A415B-6922-43F2-9DEE-F012DE1497BA}" ph="main" uid="{B14A545D-F2F1-4AAB-8463-8034ABD06826}"/>
    <r id="{FDF0D36C-1863-4DAA-BB68-555A2F3A270A}" ph="tab_menu" uid="{16A7A1FE-5FB4-4394-B053-58147F3B584D}"/>
    <r id="{1778C38C-071A-4219-A141-4686B095E361}" ph="/body/main/tab_content" uid="{4CD0B2F1-CAC8-4C03-B11E-56AE8A2589E1}"/>
    <r id="{E863286C-76C6-4078-A718-09E0AF129D68}" ph="foot" uid="{D6163B3C-0B93-4ADE-8657-E6AB4DF84B1E}"/>
    <r uid="{26A873D4-2928-401D-A5AF-F71041A65AB9}" cac="0" cnd="" mvt="" ds="" id="{2834C1FA-1150-4784-B4DF-A743DB3EF0F4}" par="" ph="special_rates_sidebar" vbd="0" vbdev="0" vbl="0" vbp="0" vbqs="0" vbu="0"/>
</d>
<d id="{46D2F427-4CE5-4E1F-BA10-EF3636F43534}" l="{1B03BFE2-32C4-4C12-A4AE-C966CEB3F7FD}">
    <r id="{E7FB95F1-9E08-412E-A9DF-7F338C74D220}" ph="head" uid="{C409527B-CDC3-4D20-B9DD-27DE389904C7}"/>
    <r cac="0" cnd="" mvt="" ds="" id="{789ABFC6-2950-485D-B600-C139B7FEAA9B}" par="" ph="" uid="{E63EE34C-639D-493F-A3BC-DCF782EFC75B}" vbd="0" vbdev="0" vbl="0" vbp="0" vbqs="0" vbu="0"/>
    <r cac="0" cnd="" mvt="" ds="" id="{255C6F91-525C-4665-B897-1CF9282F59AA}" par="" ph="body" uid="{2373A2F1-3AD8-440E-B401-1169BF6BB8AA}" vbd="0" vbdev="0" vbl="0" vbp="0" vbqs="0" vbu="0"/>
</d>
</r>

This is the format I need the XML to be in for the referenced Parse() function to parse the data properly. I can't seem to get this to work in code however. I have tried the solution described here: How to remove namespace prefix. (C#) but when I call the Remove() methods the s: prefixes get converted to something like p:7.

Can someone please give me some advice on how to convert my first example to my second example.

EDIT: This is the code I have right now and the output that I get

XDocument xDoc = XDocument.Parse(renderingProperty);
xDoc.Root.RemoveAttributes();
xDoc.Root.Add(new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance\\"));
xDoc.Root.Add(new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema\\"));

OUTPUT

<r xmlns:p="p" xmlns:s="s" p:p="1">
<d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}">
    <r uid="{F72A4E1A-519B-42DA-A1E8-F7D10EA92C65}" p:before="r[@uid='{15E0854F-39D8-4890-8541-9C4102C0F944}']" s:id="{6AF25CA3-6AB7-4792-B582-5DE0DF6349D7}" s:ph="/body/main/tab_content/body_content_column"/>
    <r uid="{15E0854F-39D8-4890-8541-9C4102C0F944}" p:before="r[@uid='{7CCB5501-52DF-49F6-BE71-E7B0CA68EE3B}']" s:id="{FD61546D-1226-4292-85F2-656883507F45}" s:ph="/body/main/tab_content/body_content_column"/>
    <r uid="{7CCB5501-52DF-49F6-BE71-E7B0CA68EE3B}" p:before="r[@uid='{17AC1E20-55CB-4EA1-9073-35D9E776C9A3}']" s:id="{1778C38C-071A-4219-A141-4686B095E361}" s:ph="/body/main/tab_content"/>
    <r uid="{17AC1E20-55CB-4EA1-9073-35D9E776C9A3}" p:before="r[@uid='{9B34F931-3C72-40D2-90B7-D4DCC81163D5}']" s:id="{C1CF5E12-1793-4F2C-B49C-00D30F0F501F}" s:ph="/body/hero/booking_widget"/>
    <r uid="{9B34F931-3C72-40D2-90B7-D4DCC81163D5}" p:before="r[@uid='{957B0932-A2B0-406F-A2D4-8C37E9F9D3A7}']" s:cac="0" s:cnd="" s:mvt="" s:ds="" s:id="{DF8E2887-0BCA-4C46-B2E4-02CDD3652892}" s:par="" s:ph="/body/main/tab_content/body_tile_column" s:vbd="0" s:vbdev="0" s:vbl="0" s:vbp="0" s:vbqs="0" s:vbu="0"/>
    <r uid="{957B0932-A2B0-406F-A2D4-8C37E9F9D3A7}" p:before="r[@uid='{B6EBC78C-5C85-4256-9F9D-3DF2F907DFA6}']" s:cac="0" s:cnd="" s:mvt="" s:ds="" s:id="{69FF80E2-6E79-4556-AC95-E409121B15C7}" s:par="MapSize=mini" s:ph="/body/main/tab_content/body_tile_column" s:vbd="0" s:vbdev="0" s:vbl="0" s:vbp="0" s:vbqs="0" s:vbu="0"/>
    <r uid="{B6EBC78C-5C85-4256-9F9D-3DF2F907DFA6}" p:before="r[@uid='{6700A64F-D8B7-44D3-8BE8-A162563ADB01}']" s:cac="0" s:cnd="" s:mvt="" s:ds="" s:id="{A6F5FC25-AB71-4C3B-B7FA-EF1F790DA86A}" s:par="" s:ph="/body/main/tab_content/body_tile_column" s:vbd="0" s:vbdev="0" s:vbl="0" s:vbp="0" s:vbqs="0" s:vbu="0"/>
    <r uid="{6700A64F-D8B7-44D3-8BE8-A162563ADB01}" p:before="r[@uid='{9863B72F-AE05-4D34-B271-E1AD6D67B946}']" s:id="{0430BDF5-8E53-4B12-A8A2-C098F667E575}" s:ph="/body/main/tab_content/print_share_toolbar"/>
    <r uid="{9863B72F-AE05-4D34-B271-E1AD6D67B946}" p:before="r[@uid='{166944E6-FCEA-4CE6-A5DC-EC317337046B}']" s:id="{FDF0D36C-1863-4DAA-BB68-555A2F3A270A}" s:ph="/body/main/tab_menu"/>
    <r uid="{166944E6-FCEA-4CE6-A5DC-EC317337046B}" p:before="r[@uid='{E98B1E29-46AF-46A3-89C0-441443E97BA0}']" s:id="{F94A415B-6922-43F2-9DEE-F012DE1497BA}" s:ph="/body/main"/>
    <r uid="{E98B1E29-46AF-46A3-89C0-441443E97BA0}" p:before="r[@uid='{B03EF589-3103-448A-8105-BFC33BA953D0}']" s:id="{EAFA90C7-136A-41C8-9AE9-8B1123E7E3C9}" s:ph="body"/>
    <r uid="{969938EE-BF4F-4A42-BA30-A7AE12183B08}">
        <p:d/>
    </r>
    <r uid="{AAA093DC-A63F-4538-A94B-ECBB509008F5}">
        <p:d/>
    </r>
    <r uid="{16A7A1FE-5FB4-4394-B053-58147F3B584D}">
        <p:d/>
    </r>
    <r uid="{4CD0B2F1-CAC8-4C03-B11E-56AE8A2589E1}" p:before="*[1=2]"/>
</d>
</r>

Thanks!!



Solution 1:[1]

Your problem is that you're removing the namespace prefix from the root element, but it's still being used to qualify the attributes themselves in the rest of the document. You want to strip namespaces out of the entire document first.

public static class XmlExtensions
{
    public static void StripNamespace(this XDocument document)
    {
        if (document.Root == null) return;

        foreach (var element in document.Root.DescendantsAndSelf())
        {
            element.Name = element.Name.LocalName;
            element.ReplaceAttributes(GetAttributesWithoutNamespace(element));
        }
    }

    static IEnumerable<XAttribute> GetAttributesWithoutNamespace(XElement xElement)
    {
        return xElement.Attributes()
            .Where(x => !x.IsNamespaceDeclaration)
            .Select(x => new XAttribute(x.Name.LocalName, x.Value));
    }
}

Then simply:

XDocument xDoc = XDocument.Parse(xml);
xDoc.StripNamespace();
xDoc.Root.Add(new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance\\"));
xDoc.Root.Add(new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema\\"));

This produces the XML you were after.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community