Search code examples
c#xmlxmldocument

Create XML with XmlDocument C#


I need to create an XML with this structure :

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:res="http://resource.webservice.correios.com.br/">
   <soapenv:Header/>
   <soapenv:Body>
      <res:buscaEventos>
         <usuario>ECT</usuario>
         <senha>SRO</senha>
         <tipo>L</tipo>
         <resultado>T</resultado>
         <lingua>101</lingua>
         <objetos>JS331400752BR</objetos>
      </res:buscaEventos>
   </soapenv:Body>
</soapenv:Envelope>

However it is wrong out this way:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:res="http://resource.webservice.correios.com.br/">
  <soapenv:Header />
  <soapenv:Body>
    <res:buscaEventos xmlns:res="http://schemas.xmlsoap.org/soap/envelope/">
      <usuario>ETC</usuario>
      <senha>SRO</senha>
      <tipo>L</tipo>
      <resultado>T</resultado>
      <lingua>101</lingua>
      <objetos>JS331400752BR</objetos>
    </res:buscaEventos>
  </soapenv:Body>
</soapenv:Envelope>

The difference is in buscaEventos

I created in the following way XmlNode eventosNode = xmlDoc.CreateElement ( "res " , " buscaEventos " " http://schemas.xmlsoap.org/soap/envelope/ " ) ; How do I remove the xmlns : res only that node ?


Solution

  • The res namespace is mapped to http://resource.webservice.correios.com.br/ at the root, but when you created buscaEventos you remapped it.

    This may solve the problem:

     XmlNode eventosNode = xmlDoc.CreateElement("res", "buscaEventos"
         "http://resource.webservice.correios.com.br/" ) ;