jats
jats

Reputation: 137

How to remove prefix using c# .net

<AcknowledgementList xmlns="http://www.irs.gov/efile" xmlns:efile="http://www.irs.gov  /efile">

     <efile:Acknowledgement>
 <efile:SubmissionId>*********</efile:SubmissionId>
     <efile:EFIN>465465</efile:EFIN>
     <efile:TaxYear>2011</efile:TaxYear>
     <efile:GovernmentCode>OHST</efile:GovernmentCode>
     </efile:Acknowledgement>
</AcknowledgementList>

Can any body tell me what the best way to remove prefix efile from this?

Upvotes: 1

Views: 5590

Answers (1)

Surjit Samra
Surjit Samra

Reputation: 4662

Full working sample here

using System;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApplication5
{
    class Program
    {   
        static void Main(string[] args)
        {
            string xml = @"<AcknowledgementList xmlns=""http://www.irs.gov/efile"" xmlns:efile=""http://www.irs.gov/efile"">
            <efile:Acknowledgement>
            <efile:SubmissionId>4654652011356f7ovyf5</efile:SubmissionId>
            <efile:EFIN>465465</efile:EFIN>
            <efile:TaxYear>2011</efile:TaxYear>
            <efile:GovernmentCode>OHST</efile:GovernmentCode>
            </efile:Acknowledgement>
            </AcknowledgementList>";

            XDocument doc = XDocument.Parse(xml, LoadOptions.PreserveWhitespace);
            doc.Descendants().Attributes().Where(a => a.IsNamespaceDeclaration).Remove();
            xml = doc.ToString();
            Console.WriteLine(xml);
        }
    }    
}

your output

<AcknowledgementList xmlns="http://www.irs.gov/efile">
         <Acknowledgement xmlns="http://www.irs.gov  /efile">
         <SubmissionId>4654652011356f7ovyf5</SubmissionId>
         <EFIN>465465</EFIN>
         <TaxYear>2011</TaxYear>
         <GovernmentCode>OHST</GovernmentCode>
         </Acknowledgement>
         </AcknowledgementList>

Upvotes: 11

Related Questions