2011-12-26 63 views
1
<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的最佳方法?如何刪除前綴使用c#.net

+0

你或許能成爲一個更確切的一點關於你希望做什麼?這是一個字符串,你想刪除所有的「efile」子字符串? – ken2k

+0

你是什麼意思?你想把它解析爲XML或原始字符串?你到目前爲止得到了什麼? –

+0

我想XmlDocument沒有efile前綴... – jats

回答

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>