从linq读取的Xdocument:“枚举没有产生结果” [英] Xdocument read from linq: "enumeration yielded no results"

查看:72
本文介绍了从linq读取的Xdocument:“枚举没有产生结果”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用XDocument阅读以下XML文档。见下面的代码:



I have the following XML document that I am trying to read using XDocument. See below code:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Document xmlns='urn:iso:std:iso:20022:tech:xsd:pain.002.001.03' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
  <CstmrPmtStsRpt>
    <GrpHdr>
      <MsgId>21169931</MsgId>
      <CreDtTm>2018-11-16T13:40:12</CreDtTm>
      <InitgPty>
        <Nm>Standard Bank SA</Nm>
        <Id>
          <OrgId>
            <BICOrBEI>SBZAZAJJXXX</BICOrBEI>
          </OrgId>
        </Id>
      </InitgPty>
    </GrpHdr>
    <OrgnlGrpInfAndSts>
      <OrgnlMsgId>KESD00017-Domestic Base-300732</OrgnlMsgId>
      <OrgnlMsgNmId>PAIN.001.001.03</OrgnlMsgNmId>
      <OrgnlCreDtTm>2018-11-16T11:11:25</OrgnlCreDtTm>
      <OrgnlNbOfTxs>1</OrgnlNbOfTxs>
      <OrgnlCtrlSum>15000000.00</OrgnlCtrlSum>
      <GrpSts>RJCT</GrpSts>
      <StsRsnInf>
        <Rsn>
          <Cd>NARR</Cd>
        </Rsn>
        <AddtlInf>Error: 4022</AddtlInf>
        <AddtlInf>Duplicate File</AddtlInf>
      </StsRsnInf>
    </OrgnlGrpInfAndSts>
  </CstmrPmtStsRpt>
</Document>





我需要从2个标签中获取信息,即:< orgnlmsgid>和< addtlinf>



因此数据应该像



string id =KESD00017-Domestic Base-300732;

string info =错误:4022,重复文件; // Concat都用逗号分隔。



我怎样才能实现这个目标?



我尝试了什么:



我已经尝试了下面的id标签,但我收到通知:空=枚举没有结果



var id =(

来自doc.Descendants中的c(OrgnlGrpIn​​fAndSts)

选择c.Element(OrgnlMsgId)。值

);



I need to get the information from only 2 tags, namely: <orgnlmsgid> and <addtlinf>

The data should thus be like

string id = "KESD00017-Domestic Base-300732";
string info = "Error: 4022, Duplicate File"; // Concat both separated with a comma.

How can I achieve this?

What I have tried:

I have tried the below just for the id tag but I get the notification: "Empty = "Enumeration yielded no results"

var id = (
from c in doc.Descendants("OrgnlGrpInfAndSts")
select c.Element("OrgnlMsgId").Value
);

推荐答案

有一个默认的命名空间



每次指定元素名称时,都需要包含命名空间:

There is a default namespace.

Everytime you specify an element name, you need to include the namespace:
            XNamespace ns = "urn:iso:std:iso:20022:tech:xsd:pain.002.001.03";
...
            doc.Descendants(ns + "OrgnlGrpInfAndSts");
...


这篇关于从linq读取的Xdocument:“枚举没有产生结果”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆