Parse XML linq namespace issue

匆匆过客 提交于 2019-12-24 13:46:59

问题


Follwoing the XML I am parsing using Linq to XML. Using below code but getting nothing.

<?xml version="1.0" encoding="utf-8"?> 
    <!--ADOR Acknowledgement 2-->  
    <AckTransmission xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.irs.gov/efile"> 
      <TransmissionHeader recordCount="1"> 
        <Jurisdiction>ALABAMA</Jurisdiction> 
        <TransmissionId>1946157056</TransmissionId> 
        <Timestamp>2012-08-16T01:25:47-05:00</Timestamp> 
        <Transmitter> 
          <ETIN>00000</ETIN> 
        </Transmitter> 
        <ProcessType>T</ProcessType> 
        <AgentIdentifier>ACK</AgentIdentifier> 
      </TransmissionHeader> 
      <Acknowledgement> 
        <SubmissionId>X1684956672</SubmissionId> 
        <EFIN>X16849</EFIN> 
        <GovernmentCode>ALST</GovernmentCode> 
        <SubmissionType>XMLTOM</SubmissionType> 
        <TaxYear>9999</TaxYear> 
        <SubmissionCategory>MFET</SubmissionCategory> 
        <AcceptanceStatus>A</AcceptanceStatus> 
        <ContainedAlerts>0</ContainedAlerts> 
        <StatusDate>2012-08-16</StatusDate> 
      </Acknowledgement> 
    </AckTransmission>

I am trying use Linq to parse this XML

using

var 

q1 = from c1 in TheDocument1.Descendants("{http://www.irs.gov/efile}AckTransmission")
                         select new
                         {
                             Jurisdiction = c1.Element("{http://www.irs.gov/efile}Jurisdiction").Value,
                             TransmissionId = c1.Element("{http://www.irs.gov/efile}TransmissionId").Value,
                             ETIN = c1.Element("{http://www.irs.gov/efile}Transmitter").Element("{http://www.irs.gov/efile}ETIN").Value,
                             lTimestamp = c1.Element("{http://www.irs.gov/efile}Timestamp").Value,
                             ProcessType = c1.Element("{http://www.irs.gov/efile}ProcessType").Value,
                             AgentIdentifie = c1.Element("{http://www.irs.gov/efile}AgentIdentifier").Value,

                             GovernmentCode = c1.Element("{http://www.irs.gov/efile}GovernmentCode").Value,
                             SubmissionType = c1.Element("{http://www.irs.gov/efile}SubmissionType").Value,
                             TaxYear = c1.Element("{http://www.irs.gov/efile}TaxYear").Value,
                             StatusDate = c1.Element("{http://www.irs.gov/efile}StatusDate").Value
                         };

But getting nothing I tried lot of things and lost. Please help me. Thanks in advance


回答1:


Create namespace object and use it with element/node.

 XNamespace ns = "http://www.irs.gov/efile";
 XDocument doc=XDocument.Load(file);


 var result = doc.Descendants(ns + "AckTransmission").Select(p => new
 {
  Jurisdiction = (string)p.Element(ns + "TransmissionHeader").Element(ns + "Jurisdiction"),
  TransmissionId = (string)p.Element(ns + "TransmissionHeader").Element(ns + "TransmissionId")
 });


来源:https://stackoverflow.com/questions/12257111/parse-xml-linq-namespace-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!