Extract attributes from an XML in a SOAP message

Asked

Viewed 588 times

0

I am building a Webservice that returns status of an object, so I get the data of the object to search in 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 take this message quietly, but, I’m picking up a little bit to get the values of the nodes that are inside the arg0 node, I’m trying so:

        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 that? If there is another way where I don’t need to manually select attributes, but do the conversion directly to a class, I also accept.

  • It would not be an option to work with serialization and deserialization of your Xml https://answall.com/a/44245/5846 https://answall.com/q/13087/5846

1 answer

1


In your case, I suggest you use the LINQ to select the nodes you want. Since, it is not too difficult to manipulate XML with it.

See how to get the values with LINQ through this your XML, follow the code:

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}");
});

Exit:

Type Object: C
Code object: 999999
Value: 9999999999
Name subject: xxxxxxxxxxxx

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

See the program working on .NET Fiddle.

  • I ended up solving it another way, but, I’ll test yours and I’ll tell you later.

Browser other questions tagged

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