使用linq调用xml标记值

时间:2016-03-22 15:44:52

标签: xml linq

<?xml version="1.0" encoding="UTF-8"?>
<CBISDDReqLogMsg xmlns="urn:CBI:xsd:CBISDDReqLogMsg.00.01.00" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:CBI:xsd:CBISDDReqLogMsg.00.01.00 CBISDDReqLogMsg.00.01.00.xsd">
  <GrpHdr>
    <PmtInf>
      <PmtInfId>1</PmtInfId>
      <PmtMtd>DD</PmtMtd>
      <BtchBookg>false</BtchBookg>
      <PmtTpInf>
        <SvcLvl>
          <Cd>SEPA</Cd>
        </SvcLvl>
        <LclInstrm>
          <Cd>CORE</Cd>
        </LclInstrm>
        <SeqTp>RCUR</SeqTp>
      </PmtTpInf>
      <ReqdColltnDt>2015-12-11</ReqdColltnDt>
      <Cdtr>
        <Nm>AUTOMOBILE CLUB SAVONA</Nm>
        <PstlAdr>
          <TwnNm>SAVONA</TwnNm>
          <Ctry>IT</Ctry>
          <AdrLine>VIA GUIDOBONO 23</AdrLine>
        </PstlAdr>
        <Id>
          <PrvtId>
            <Othr>
              <Id>IT050020000000405030586</Id>
            </Othr>
          </PrvtId>
        </Id>
      </Cdtr>
      <CdtrAcct>
        <Id>
          <IBAN/>
        </Id>
      </CdtrAcct>
      <CdtrAgt>
        <FinInstnId>
          <ClrSysMmbId>
            <MmbId>05584</MmbId>
          </ClrSysMmbId>
        </FinInstnId>
      </CdtrAgt>
      <CdtrSchmeId>
        <Nm>AUTOMOBILE CLUB SAVONA</Nm>
        <Id>
          <PrvtId>
            <Othr>
              <Id>IT050020000000405030586</Id>
            </Othr>
          </PrvtId>
        </Id>
      </CdtrSchmeId>
      <DrctDbtTxInf>
        <PmtId>
          <InstrId>1</InstrId>
          <EndToEndId>000001</EndToEndId>
        </PmtId>
        <InstdAmt Ccy="EUR">59</InstdAmt>
        <DrctDbtTx>
          <MndtRltdInf>
            <MndtId>**7F4934SV002201220</MndtId>
            <DtOfSgntr>2012-03-22</DtOfSgntr>
            <AmdmntInd>false</AmdmntInd>
          </MndtRltdInf>
        </DrctDbtTx>
      </DrctDbtTxInf>
    </PmtInf>
  </GrpHdr>
</CBISDDReqLogMsg>

在xml文件的基础上,我必须通过标记<InstdAmt>(最后一部分)获取标记<MndtId>的值。 ID正确,但当我返回时,字符串告诉我对象没有设置为对象的实例....放在linq代码下。你有解决方案吗? 非常感谢。

   private string ImportoXML(string PercorsoXML, string ID)
    {
        XElement xdoc = XElement.Load(PercorsoXML); //you'll have to edit your path

        //Run query
        var ElencoValori = from lv2 in xdoc.Descendants("PmtInf").Elements("DrctDbtTxInf")
                           select new
                           {
                               //PmtId = lv2.Element ("PmtId").Element("InstrId").Value,
                               //InstdAmt = lv2.Element ("InstdAmt").Value
                               PmtId = lv2.Element("DrctDbtTx")
                                           .Element("MndtRltdInf")
                                           .Element("MndtId")
                                           .Value,
                               InstdAmt = lv2.Element("InstdAmt").Value
                           };
        return ElencoValori.Where(c => c.PmtId.EndsWith(ID)).FirstOrDefault().InstdAmt.ToString();

    }

1 个答案:

答案 0 :(得分:0)

定义您的命名空间,然后在您的查询中使用它

XNamespace  ns = @"urn:CBI:xsd:CBISDDReqLogMsg.00.01.00";
var ElencoValori = from lv2 in xdoc.Descendants(ns+"PmtInf").Elements(ns + "DrctDbtTxInf")
                           select new
                           {
                               //PmtId = lv2.Element ("PmtId").Element("InstrId").Value,
                               //InstdAmt = lv2.Element ("InstdAmt").Value
                               PmtId = lv2.Element(ns + "DrctDbtTx")
                                           .Element(ns + "MndtRltdInf")
                                           .Element(ns + "MndtId")
                                           .Value,
                               InstdAmt = lv2.Element(ns + "InstdAmt").Value
                           };
return ElencoValori.Where(c => c.PmtId.EndsWith(ID)).FirstOrDefault().InstdAmt.ToString();