Tag ns1: repeats in all other tags (XML)

0

I'm trying to make an XML but the ns1 tag repeats all in the ready xml, can you solve this in an easier way?

Code sample:

function TRPS.geraXML:TStrings;
var XMLDoc    : TXMLDocument;
ANode     : IXMLNode;
begin
try
   XMLDoc     := TXMLDocument.Create(Application);
   With XMLDoc do
   begin
     Active   := True;
     Version  := '1.0';
     Encoding := 'UTF-8';

     AddChild('ns1:ReqEnvioLoteRPS');
     DocumentElement.Attributes['xmlns:ns1']          := 'http://localhost:8080/WsNFe2/lote';
     DocumentElement.Attributes['xmlns:tipos']        := 'http://localhost:8080/WsNFe2/tp';
     DocumentElement.Attributes['xmlns:xsi']          := 'http://www.w3.org/2001/XMLSchema-instance';
     DocumentElement.Attributes['xsi:schemaLocation'] := 'http://localhost:8080/WsNFe2/lote http://localhost:8080/WsNFe2/xsd/ReqEnvioLoteRPS.xsd';

    ANode := DocumentElement;
    with ANode.addChild('Cabecalho') do
    begin
       AddChild('CodCidade').NodeValue            := Self.FPrestadorCidadeCodigoIBGE;
       AddChild('CPFCNPJRemetente').NodeValue     := Self.FPrestadorCPFCNPJ;
       AddChild('RazaoSocialRemetente').NodeValue := Self.FPrestadorRazaoSocial;
       AddChild('Transacao').NodeValue            := Self.FRPSTransacao;
       AddChild('DTInicio').NodeValue             := FormatDateTime('yyyy-mm-dd',Self.FLoteDtInicio);
       AddChild('DTFim').NodeValue                := FormatDateTime('yyyy-mm-dd',Self.FLoteDTFim);
       AddChild('QtdRPS').NodeValue               := Self.FLoteQtdeRPS;
       AddChild('ValorTotalServicos').NodeValue   := VirgulaPorPonto(FormatFloat('#0.00',Self.FLoteValorTotalServicos));
       AddChild('ValorTotalDeducoes').NodeValue   := VirgulaPorPonto(FormatFloat('#0.00',Self.FLoteValorTotalDeducoes));
       AddChild('Versao').NodeValue               := Self.LoteVersao;
       AddChild('MetodoEnvio').NodeValue          := Self.FLoteMetodoEnvio;
   end;
   Result := XMLDoc.XML;
finally
  FreeAndNil(XMLDoc);
end;
end;

Sample delivered XML has n1 tag everywhere

   <?xml version="1.0"?>

   -<ns1:ReqEnvioLoteRPS xsi:schemaLocation="http://localhost:8080/WsNFe2/lote http://localhost:8080/WsNFe2/xsd/ReqEnvioLoteRPS.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tipos="http://localhost:8080/WsNFe2/tp" xmlns:ns1="http://localhost:8080/WsNFe2/lote">

   -<ns1:Cabecalho>
       <ns1:CodCidade>0</ns1:CodCidade>
       <ns1:CPFCNPJRemetente/>
       <ns1:RazaoSocialRemetente/>
       <ns1:Transacao/>
       <ns1:DTInicio>1899-12-30</ns1:DTInicio>
       <ns1:DTFim>1899-12-30</ns1:DTFim>
       <ns1:QtdRPS>0</ns1:QtdRPS>
       <ns1:ValorTotalServicos>0.00</ns1:ValorTotalServicos>
       <ns1:ValorTotalDeducoes>0.00</ns1:ValorTotalDeducoes>
       <ns1:Versao>0</ns1:Versao>
       <ns1:MetodoEnvio/>
       </ns1:Cabecalho>
   </ns1:ReqEnvioLoteRPS>
    
asked by anonymous 15.07.2014 / 00:10

1 answer

1

Remove the: ns1 from here: DocumentElement.Attributes ['xmlns: ns1']: = It is from here: AddChild ('ns1: ReqLoadLoteRPS'). Since you are basing your xml on a schema, it may be that your framework still manages xml with this namespace indicator. It is not wrong what you want to do, but it would be interesting to keep ns1, since it indicates which xml schema the complex type belongs to. This is a good practice, since it can happen that in the same xml there are two or more complex types, which can even conflict. And without the indication of the origin, the interpretation of its xml is impaired. Take a look at this link for the best and most complete explanation: link

    
01.08.2014 / 00:20