IRestResponse读取List数据

问题描述 投票:0回答:1

这是我第一次使用 SOAP.. 我使用了一个 Rest 请求,并收到了这样的 SOAP:

    <SOAP-ENV:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c1x="http://www.xxxxxxxxx.Request.com" xmlns:chan="http://www.ibm.com/xmlns/prod/CICS/channel-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <C1XAR080OOperationResponse xmlns="http://www.XXXXXXXXX.Response.com" xmlns:cics="http://www.ibm.com/xmlns/prod/CICS/channel-instance"  cics:channel="true">
            <DHMESSAG xmlns:cics="http://www.ibm.com/xmlns/prod/CICS/channel-instance"  cics:structuredContainer="true">
                <Context>
                    <AreaInput>
                        <ChiaveMessaggio>
                            <codiceMessaggio>XXXXXXX</codiceMessaggio>
                            <canale>V</canale>
                            <versione>01</versione>
                        </ChiaveMessaggio>
                        <tipoOperazione>S</tipoOperazione>                        
                    </AreaInput>
                    <AreaOutput>
                        <esito>C</esito>
                        <tipoErrore> </tipoErrore>
                        <tipoRisposta>C</tipoRisposta>
                        <transidHost>XX</transidHost>
                        <transazioneController>&amp;SY&amp;</transazioneController>
                        <cicsDiEsecuzione>XX</cicsDiEsecuzione>
                        <timeOutHost>60</timeOutHost>
                        <idHost>XX</idHost>
                        <messaggioErrore>                                                                                </messaggioErrore>
                        <codErrore>        </codErrore>
                        <AreaAbend>
                            <abendPGM>XX</abendPGM>
                            <abendStepPgm>X</abendStepPgm>
                            <abendStepId>XX</abendStepId>
                            <abendCode>    </abendCode>
                        </AreaAbend>
                        <taskNumero>XXXXX</taskNumero>
                        <invPgm>XXXX </invPgm>
                    </AreaOutput>
                    <contatoreRiesecuzioni>0</contatoreRiesecuzioni>
                    <delayStartTransid>  </delayStartTransid>
                    <lungMessaggioApplicativo>0</lungMessaggioApplicativo>
                    <AreaContainer>
                        <channelCICS>XXXXXX</channelCICS>
                        <numeroElementiTabella>04</numeroElementiTabella>
                        <TabellaContainer>
                            <RigaContainer>
                                <nomeContainer>XXXXXX</nomeContainer>
                                <tipoContainer>X</tipoContainer>
                            </RigaContainer>
                            <RigaContainer>
                                <nomeContainer>XXXXXXX</nomeContainer>
                                <tipoContainer>I</tipoContainer>
                            </RigaContainer>
                            <RigaContainer>
                                <nomeContainer>XXXXXXX</nomeContainer>
                                <tipoContainer>O</tipoContainer>
                            </RigaContainer>
                            <RigaContainer>
                                <nomeContainer></nomeContainer>
                                <tipoContainer></tipoContainer>
                            </RigaContainer>
                            <RigaContainer>
                                <nomeContainer></nomeContainer>
                                <tipoContainer></tipoContainer>
                            </RigaContainer>                            
                        </TabellaContainer>
                    </AreaContainer>
                </Context>
            </DHMESSAG>
            <TB00803O xmlns:cics="http://www.ibm.com/xmlns/prod/CICS/channel-instance"  cics:structuredContainer="true">
                <DatiUo>
                    <DatiUoReturnCode>
                        <DatiUoErrType></DatiUoErrType>
                        <DatiUoCodErr>        </DatiUoCodErr>
                        <DatiUoMessErr>                                                                                </DatiUoMessErr>
                    </DatiUoReturnCode>
                    <DatiOutput>
                        <maxLiv>1</maxLiv>
                        <maxOccurs>974</maxOccurs>
                        <elemTabUo>
                            <codUo>1234</codUo>
                            <livello>1</livello>
                            <indPadre>0</indPadre>
                            <indFiglioFirst>2</indFiglioFirst>
                            <indFiglioLast>9</indFiglioLast>
                            <sportello> </sportello>
                            <descUo>XXXXXXXXXXXX.                              </descUo>
                            <tipoFil>D</tipoFil>
                            <canaleCiCpRczl>XX</canaleCiCpRczl>
                            <dataChiusura>01.01.0001</dataChiusura>
                            <indMail>XXXXXXXXXXXXX                                             </indMail>
                        </elemTabUo>
                        <elemTabUo>
                            <codUo>XXXXXX</codUo>
                            <livello>2</livello>
                            <indPadre>1</indPadre>
                            <indFiglioFirst>10</indFiglioFirst>
                            <indFiglioLast>110</indFiglioLast>
                            <sportello> </sportello>
                            <descUo>XXXXXXXXXXXXXX                                    </descUo>
                            <tipoFil>A</tipoFil>
                            <canaleCiCpRczl>XX</canaleCiCpRczl>
                            <dataChiusura>01.01.0001</dataChiusura>
                            <indMail>XXXXXXXXXXXXXX                                        </indMail>
                        </elemTabUo>
                    </DatiOutput>
                </DatiUo>
            </TB00803O>
        </C1XAR080OperationResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我需要将列表读入节点 TB00803O > DatiUO > DatiOutput > elemTabUo

这是读取节点 elemTabUo 的代码,但不起作用,我收到错误: 未将对象引用设置为对象的实例。

IRestResponse<TB00803O> restResponse2 = client.Execute<TB00803O>((IRestRequest)request);
//maxOccurs work, I get number with children
TextBox1.Text = restResponse2.Data.DatiUo.DatiOutput.maxOccurs;

//If I try to read first children I get error
TextBox1.Text = restResponse2.Data.DatiUo.DatiOutput.elemTabUo[0].codUo;

//I get the same error also I fi try with foreach and I see only FIRST LINE
foreach (var val in restResponse2.Data.DatiUo.DatiOutput.elemTabUo)
{
    TextBox1.Text = "---FIRST LINE---\n";
    TextBox1.Text = TextBox1.Text + val.codUo;
}

我不明白为什么我无法读取List elemTabUo,但我可以读取maxOccurs...

感谢您的帮助

c# rest soap
1个回答
0
投票

我在此链接中找到了一个很好的解决方案:parse Soap XML Response using c# at child Nodes

我创建了一个类并使用了 xml linq。

这是正确的代码:

 List<XElement> xEvents = doc.Descendants().Where(x => x.Name.LocalName == "elemTabUo").ToList();
            ns = xEvents.FirstOrDefault().GetDefaultNamespace();

            List<elemTabUo> events = xEvents.Select(x => new elemTabUo()
            {
                codUo = (string)x.Element(ns + "codUo"),
                livello = (string)x.Element(ns + "livello"),
                indPadre = (string)x.Element(ns + "indPadre"),
                indFiglioLast = (string)x.Element(ns + "indFiglioLast"),
                sportello = (string)x.Element(ns + "sportello"),
                descUo = (string)x.Element(ns + "descUo"),
                tipoFil = (string)x.Element(ns + "tipoFil"),
                canaleCiCpRczl = (string)x.Element(ns + "canaleCiCpRczl"),
                dataChiusura = (string)x.Element(ns + "dataChiusura"),
                indMail = (string)x.Element(ns + "indMail"),
            }).ToList();

            TextBox1.Text ="Tot: " + events.Count.ToString();
            TextBox1.Text = TextBox1.Text + "---INIZIO UO---\n";

            int indice = 0;

            foreach (var val in events)
            {
                indice++;
                TextBox1.Text =TextBox1.Text + "\n" + indice + " " + val.codUo;

            }


 public class elemTabUo
    {
        public string codUo { get; set; }
        public string livello { get; set; }
        public string indPadre { get; set; }
        public string indFiglioFirst { get; set; }
        public string indFiglioLast { get; set; }
        public string sportello { get; set; }
        public string descUo { get; set; }
        public string tipoFil { get; set; }
        public string canaleCiCpRczl { get; set; }
        public string dataChiusura { get; set; }
        public string indMail { get; set; }

    }
© www.soinside.com 2019 - 2024. All rights reserved.