Getting a string from an XML that I received from an http request

0

So, the problem at hand now is that I have to get two rows that are in an XML, I get that XML through an httpRequest, and the problem apparently starts.

What I've tried so far was:

...

[WebMethod]
public void PassaProtocolo(int numProtocolo) {
           string url = System.Web.Configuration.WebConfigurationManager.AppSettings.Get("consulta");
            url = url + numProtocolo.ToString();//adiciona numero do protocolo no endereço do http request

            WebRequest req = WebRequest.Create(url);//acessa httprequest
            WebResponse resp = req.GetResponse();//recebe pagina

            XDocument XmlDoc = XDocument.Load(url);
            string funfou = XmlDoc.Root.ToString();

}

...

The address I'm using as the url variable there has already been tested and returns the page as it should:

<RetornoOfConsultaProtocoloResultulvhizZy><Codigo i:nil="true"/><Descricao i:nil="true"/><Lista><d2p1:ConsultaProtocoloResult><d2p1:cNPJField/><d2p1:cPFField>34780831687</d2p1:cPFField><d2p1:codAssuntoField>17</d2p1:codAssuntoField><d2p1:codAssuntoFieldSpecified>true</d2p1:codAssuntoFieldSpecified>

-numberThere2017-02-24T00: 00: 00truenumeroaqui0true

When I run the code I get the error:

System.Xml.XmlException: Data at the root root is invalid. Line 1, position 1.    in System.Xml.XmlTextReaderImpl.Throw (Exception e)    in System.Xml.XmlTextReaderImpl.Throw (String res, String arg)    at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace ()    at System.Xml.XmlTextReaderImpl.ParseDocumentContent ()    at System.Xml.XmlTextReaderImpl.Read ()    in System.Xml.Linq.XDocument.Load (XmlReader reader, LoadOptions options)    in System.Xml.Linq.XDocument.Load (String uri, LoadOptions options)    in System.Xml.Linq.XDocument.Load (String uri)    in C: \ Users \ marcus \ Dropbox \ Projects \ Stoque \ Clients \ CREA-MG \ CautionCREA_HOMOLOGACAO \ wsIntegraCREA \ QuerySGAasmx.asmx.cs: line 41

This occurs when XDocument tries to load the url. I've never tried anything like that before, so I'm having a bit of a hard time, thanks for the help.

    
asked by anonymous 01.12.2017 / 14:20

1 answer

0

I first believe that your url variable is not your XML returned by the web service and yes you should have Xml on your response. p>

WebRequest req = WebRequest.Create(url);//acessa httprequest
WebResponse resp = req.GetResponse();//recebe pagina

XDocument xmlDoc = XDocument.Load(new StreamReader(resp.GetResponseStream()));
string funfou = xmlDoc.Root.ToString();
    
01.12.2017 / 14:32