Extract attributes from an XML in a SOAP message

0

I'm building a WebService that returns status of an object, so I get the object's data to search the base via SOAP message:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:agr="http://site.temp.br/"> 
   <soapenv:Header/> 
   <soapenv:Body> 
      <agr:getObjeto_V1> 
         <arg0> 
            <TipoObjeto>C</TipoObjeto> 
            <CodigoObjeto>999999</CodigoObjeto> 
            <Valor>9999999999999</Valor> 
            <NomeObjeto>xxxxxxxxxxxx</NomeObjeto> 
         </arg0> 
      </agr:getObjeto_V1> 
   </soapenv:Body> 
</soapenv:Envelope> 

This is the soap message sent to the webservice, in the service I get this message quietly, but, I'm getting a bit to get the values of the nodes that are inside the arg0 node, I'm trying like this:

        XmlDocument xml = new XmlDocument();
        xml.LoadXml(xmlSoapRequest.InnerXml);

        XmlNodeList xnList = xml.SelectNodes("/Envelope/Body/getObjeto_V1/arg0");
        foreach (XmlNode xn in xnList)
        {
            char TipoObjeto = (char)xn["TipoObjeto"].InnerText[0];
            string CodigoObjeto = xn["CodigoObjeto"].InnerText;
            string valor = xn["Valor"].InnerText;
            string NomeObjeto = xn["NomeObjeto"].InnerText;
        }

How can I do this? If I have another way I do not have to select the attributes manually, but do the conversion directly to a class, I also accept it.

    
asked by anonymous 30.06.2017 / 06:03

1 answer

1

In this case, I suggest that you use LINQ to select the nodes you want. Since, it is not very laborious to manipulate XML with it.

Here's how to get the values with LINQ through this XML:

var xml = @"
    <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:agr=""http://site.temp.br/"">                 
       <soapenv:Header/> 
       <soapenv:Body>  
        <agr:getObjeto_V1>   
            <arg0>   
               <TipoObjeto>C</TipoObjeto>   
               <CodigoObjeto>999999</CodigoObjeto>   
               <Valor>9999999999999</Valor>   
               <NomeObjeto>xxxxxxxxxxxx</NomeObjeto>   
            </arg0>   
         </agr:getObjeto_V1>
       </soapenv:Body>
     </soapenv:Envelope>
";            

var objs = (
            from p in XElement.Parse(xml).Descendants("arg0")
            select new
            {
                TipoObjeto = (string) p.Element("TipoObjeto"),
                CodigoObjeto = (string) p.Element("CodigoObjeto"),
                Valor = (string) p.Element("Valor"),
                NomeObjeto = (string) p.Element("NomeObjeto")
            }
          ).ToList();

objs.ForEach(x => 
{
    WriteLine($"Tipo objeto: {x.TipoObjeto}");
    WriteLine($"Codigo objeto: {x.CodigoObjeto}");
    WriteLine($"Valor: {x.Valor}");
    WriteLine($"Nome objeto: {x.NomeObjeto}");
});

Output:

  

Object type: C
  Product code: 999999
  Value: 9999999999999
  Name object: xxxxxxxxxxxx

I used the Descendants to return the descending elements of the specified element, which is the arg0 element, and transformed them and a list containing a collection of anonymous type objects.

See the program working at .NET Fiddle .

    
01.07.2017 / 05:00