Get data return xml

Asked

Viewed 438 times

1

I am trying to return the data of this xml:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<GerarNfseResposta xmlns="http://www.betha.com.br/e-nota-contribuinte-ws">
    <ListaMensagemRetorno>
        <MensagemRetorno>
            <Codigo>00000</Codigo>
            <Mensagem>00000 - O RPS 8964 da série 999 já foi informado em outra nota fiscal.</Mensagem>
        </MensagemRetorno>
    </ListaMensagemRetorno>
</GerarNfseResposta>

I’m trying to do it this way:

WebResponse webResponse = webRequest.GetResponse();
StreamReader rd = new StreamReader(webResponse.GetResponseStream());
soapResult = rd.ReadToEnd();

XmlDocument xmlResponse = new XmlDocument();
xmlResponse.LoadXml(soapResult);
XmlNode responseNode = xmlResponse.LastChild.LastChild.FirstChild;

XmlNamespaceManager ns = new XmlNamespaceManager(xmlResponse.NameTable);
ns.AddNamespace("ns2", "http://www.betha.com.br/e-nota-contribuinte-ws");
XmlNode codigo = xmlResponse.SelectSingleNode("//ns2:Codigo", ns);
XmlNode mensagem = xmlResponse.SelectSingleNode("//ns2:Mensagem", ns);

But the code and message always comes null.

I tried this way below:

 string soapResult = string.Empty;

        WebResponse webResponse = webRequest.GetResponse();
        StreamReader rd = new StreamReader(webResponse.GetResponseStream());
        soapResult = rd.ReadToEnd();

        XmlDocument xmlResponse = new XmlDocument();
        xmlResponse.LoadXml(soapResult);
        XNamespace ns = XNamespace
.Get("http://www.betha.com.br/e-nota-contribuinte-ws");

         soapResult = System.IO.File.ReadAllText(soapResult,
            System.Text.Encoding.GetEncoding("ISO-8859-1"));

        var xDoc = XDocument.Parse(soapResult)
                .Descendants(ns + "ListaMensagemRetorno")
                .Elements(ns + "MensagemRetorno")
                .Select(x => new
                {
                    Codigo = x.Element(ns + "Codigo")?.Value,
                    Mensagem = x.Element(ns + "Mensagem")?.Value
                })
                .ToList();

But it wasn’t valid either, I tried taking that line soapResult = System.IO.File.ReadAllText(soapResult, System.Text.Encoding.GetEncoding("ISO-8859-1")); but comes null

I tried to take it this way too and it didn’t work:

System.Xml.XmlNode Gerar = xmlResponse.SelectSingleNode("GerarNfseResposta"); System.Xml.XmlNode ListaMensagemRetorno = Gerar.SelectSingleNode("ListaMensagemRetorno"); System.Xml.XmlNode MensagemRetorno = ListaMensagemRetorno.SelectSingleNode("MensagemRetorno")

I tried this way below too, but always comes null.

XmlNodeList xnList = xmlResponse.SelectNodes("/GerarNfseResposta/ListaMensagemRetorno/MensagemRetorno");
        foreach (XmlNode xn in xnList)
        {
            string firstName = xn["Codigo"].InnerText;
            string lastName = xn["Mensagem"].InnerText;
            Console.WriteLine("Name: {0} {1}", firstName, lastName);
        }

XML return:

 <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Header></env:Header><env:Body><ns2:GerarNfseResponse xmlns:ns2="http://www.betha.com.br/e-nota-contribuinte-ws"><return>&lt;?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?&gt;
&lt;GerarNfseResposta xmlns="http://www.betha.com.br/e-nota-contribuinte-ws"&gt;
    &lt;ListaMensagemRetorno&gt;
        &lt;MensagemRetorno&gt;
            &lt;Codigo&gt;00000&lt;/Codigo&gt;
            &lt;Mensagem&gt;00000 - O RPS 8965 da série 999 já foi informado em outra nota fiscal.&lt;/Mensagem&gt;
        &lt;/MensagemRetorno&gt;
    &lt;/ListaMensagemRetorno&gt;
&lt;/GerarNfseResposta&gt;
</return></ns2:GerarNfseResponse></env:Body></env:Envelope>
  • this question XML contains problems, it is difficult to reproduce the problem.

  • @Virgilionovic this xml is what webservice returns to me.

  • So its shape is invalid ... !!!

  • I can’t get his feedback ?

  • An example of the problem, a tag: that is from the top <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> of any xml file, it’s in the middle... .

  • @Virgilionovic edited the question with the correct feedback, sorry.

Show 1 more comment

1 answer

2


A solution is with Xdocument from the namespace System.xml.Linq, example:

XNamespace ns = XNamespace
    .Get("http://www.betha.com.br/e-nota-contribuinte-ws"); 

string soapResult = System.IO.File.ReadAllText(@"./data.xml", 
    System.Text.Encoding.GetEncoding("ISO-8859-1"));

var xDoc = XDocument.Parse(soapResult, LoadOptions.None)
        .Descendants(ns + "ListaMensagemRetorno")
        .Elements(ns + "MensagemRetorno")
        .Select(x => new
        {
            Codigo = x.Element(ns + "Codigo")?.Value,
            Mensagem = x.Element(ns + "Mensagem")?.Value
        })
        .ToList();

Remembering that any who owns a namespace to retrieve your information should also be passed. Another factor is that has the enconding ISO-8859-1 that needs to be respected and its loading must also be configured, being responsible for the accents and special characters.

References:

  • I managed to decode, but it returns me this error: Unexpected XML declaration. The XML declaration must be the first Node in the Document, and no whitespace characters are allowed to appear before it. Line 1, position 196.

  • 1

    I had to make a replace on this line <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> and then it worked. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.