title: How to strip namespaces from Xml Document
body: <p>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:</p>
  
  <pre><code>&lt;r xmlns:p="p" xmlns:s="s" p:p="1"&gt;
  &lt;d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}"&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;r uid="{969938EE-BF4F-4A42-BA30-A7AE12183B08}"&gt;
          &lt;p:d/&gt;
      &lt;/r&gt;
      &lt;r uid="{AAA093DC-A63F-4538-A94B-ECBB509008F5}"&gt;
          &lt;p:d/&gt;
      &lt;/r&gt;
      &lt;r uid="{16A7A1FE-5FB4-4394-B053-58147F3B584D}"&gt;
          &lt;p:d/&gt;
      &lt;/r&gt;
      &lt;r uid="{4CD0B2F1-CAC8-4C03-B11E-56AE8A2589E1}" p:before="*[1=2]"/&gt;
  &lt;/d&gt;
  &lt;/r&gt;
  </code></pre>
  
  <p>I am using an edit utility to view the above XML in HTML format that shows this same output as:</p>
  
  <pre><code>&lt;r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  &lt;d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}" l="{EAB1DFDD-9918-4158-862B-BC3FD02B00CC}"&gt;
      &lt;r id="{FC207214-4E05-40E9-B3C5-49AE41B6A323}" ph="head" uid="{B03EF589-3103-448A-8105-BFC33BA953D0}"/&gt;
      &lt;r id="{EAFA90C7-136A-41C8-9AE9-8B1123E7E3C9}" ph="body" uid="{969938EE-BF4F-4A42-BA30-A7AE12183B08}"/&gt;
      &lt;r id="{9325E0F7-A4B5-46EC-B178-E7EF051FB1FE}" ph="hero" uid="{6C998C3F-C968-466A-AB3A-C9C12C78A389}"/&gt;
      &lt;r id="{7728075C-15DE-4DBE-9018-32794A1540F0}" ph="breadcrumb" uid="{593B65AF-BFA4-45F0-9EF0-12806CF54468}"/&gt;
      &lt;r id="{C1CF5E12-1793-4F2C-B49C-00D30F0F501F}" ph="booking_widget" uid="{AAA093DC-A63F-4538-A94B-ECBB509008F5}"/&gt;
      &lt;r id="{2BA579A5-06C8-41D7-B170-EA745D4CDE8E}" ph="image_rotator" uid="{CC96DC9A-60EB-4C68-8F32-06838EB69818}"/&gt;
      &lt;r uid="{F8E91E5B-1C55-42F6-96C2-52B1FE86ECEA}" id="{C8B42985-3D99-4E08-951C-2C3A98A737B4}" ph="main"/&gt;
      &lt;r uid="{209FDA28-CA6D-4306-9587-294E35849620}" id="{0430BDF5-8E53-4B12-A8A2-C098F667E575}" ph="print_share_toolbar"/&gt;
      &lt;r id="{F94A415B-6922-43F2-9DEE-F012DE1497BA}" ph="main" uid="{B14A545D-F2F1-4AAB-8463-8034ABD06826}"/&gt;
      &lt;r id="{FDF0D36C-1863-4DAA-BB68-555A2F3A270A}" ph="tab_menu" uid="{16A7A1FE-5FB4-4394-B053-58147F3B584D}"/&gt;
      &lt;r id="{1778C38C-071A-4219-A141-4686B095E361}" ph="/body/main/tab_content" uid="{4CD0B2F1-CAC8-4C03-B11E-56AE8A2589E1}"/&gt;
      &lt;r id="{E863286C-76C6-4078-A718-09E0AF129D68}" ph="foot" uid="{D6163B3C-0B93-4ADE-8657-E6AB4DF84B1E}"/&gt;
      &lt;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"/&gt;
  &lt;/d&gt;
  &lt;d id="{46D2F427-4CE5-4E1F-BA10-EF3636F43534}" l="{1B03BFE2-32C4-4C12-A4AE-C966CEB3F7FD}"&gt;
      &lt;r id="{E7FB95F1-9E08-412E-A9DF-7F338C74D220}" ph="head" uid="{C409527B-CDC3-4D20-B9DD-27DE389904C7}"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
  &lt;/d&gt;
  &lt;/r&gt;
  </code></pre>
  
  <p>This is the format I need the XML to be in for the referenced <code>Parse()</code> 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: <a href="https://stackoverflow.com/questions/3052030/how-to-remove-namespace-prefix-c">How to remove namespace prefix. (C#)</a> but when I call the <code>Remove()</code> methods the <code>s:</code> prefixes get converted to something like <code>p:7</code>. </p>
  
  <p>Can someone please give me some advice on how to convert my first example to my second example. </p>
  
  <p><strong>EDIT: This is the code I have right now and the output that I get</strong></p>
  
  <pre><code>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\\"));
  </code></pre>
  
  <p><strong>OUTPUT</strong></p>
  
  <pre><code>&lt;r xmlns:p="p" xmlns:s="s" p:p="1"&gt;
  &lt;d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}"&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;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"/&gt;
      &lt;r uid="{969938EE-BF4F-4A42-BA30-A7AE12183B08}"&gt;
          &lt;p:d/&gt;
      &lt;/r&gt;
      &lt;r uid="{AAA093DC-A63F-4538-A94B-ECBB509008F5}"&gt;
          &lt;p:d/&gt;
      &lt;/r&gt;
      &lt;r uid="{16A7A1FE-5FB4-4394-B053-58147F3B584D}"&gt;
          &lt;p:d/&gt;
      &lt;/r&gt;
      &lt;r uid="{4CD0B2F1-CAC8-4C03-B11E-56AE8A2589E1}" p:before="*[1=2]"/&gt;
  &lt;/d&gt;
  &lt;/r&gt;
  </code></pre>
  
  <p>Thanks!!</p>