XML tags being generated with prefixes

Asked

Viewed 172 times

3

I am creating an XML file by Delphi and if I use the prefix when creating the first tag, the prefix "ns1" is added to all the tags that will come below, generating error in the integration with Webservice. What I need is for the first tag to be generated with the prefix and the others not.

When I use this command when creating the first tag, the prefix is repeated in all other:

xDOC := TXMLDocument.Create(nil);
xDOC.Active := True;
xDOC.Encoding := 'UTF-8';

xNOD := xDOC.AddChild('ns1:ReqEnvioLoteRPS'); // Essa linha...
if not(xNOD.NodeName = 'ReqEnvioLoteRPS') then
  xNOD := xDOC.ChildNodes.FindNode('ReqEnvioLoteRPS');

xNOD := xNOD.AddChild('Cabecalho');

In this case, the "Header" tag is generated like this: "ns1:Cabecalho", error in the integration, as it is requested that the header be like this: "Cabecalho". If I run the line without the prefix:

xNOD := xDOC.AddChild('ReqEnvioLoteRPS'); // Essa linha...

Gives error, because the integration asks for this "ns1" in the first tag.

I need "ns1" to be informed only on the main XML tag "Reqenvioloterps", and not on the other...

1 answer

1


When you use AddChild() to add a new child tag and not explicitly specify a namespace, the child tag inherits the parent tag namespace.

This is a behavior of the own AddChild(), you cannot change it. That is why the ns1 prefix: is inherited in all child nodes.

To solve this you can make an overload of the method AddChild()

Behold this answer, is exactly the solution to your problem.

  • 1

    Right. I’ll be doing the tests here, and as soon as I can do the integration I’ll come back and mark your answer as resolved. Thank you very much!

  • 1

    Solved! Thanks a lot for the help @Lgregianin

  • 1

    I’m glad you decided.

Browser other questions tagged

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