如何使用c#.net删除前缀

时间:2011-12-26 09:15:50

标签: c#

<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>

任何机构都可以告诉我从这里删除前缀efile的最佳方法是什么?

1 个答案:

答案 0 :(得分:9)

这里有完整的工作样本

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);
        }
    }    
}

你的输出

<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>