How do I add a namespace prefix to each node using TXMLDocument

前端 未结 5 1151
轻奢々
轻奢々 2021-01-20 13:39

I used the XML Binding Wizard to create a descendant of TXMLDocument. The files generated by this class would declare the namespace in the root node and create just plain, u

5条回答
  •  忘掉有多难
    2021-01-20 14:28

    Ok, the solution took a long time to discover but was surprisingly simple.

    The code generated by XML Data Binding Wizard will create xml using the default namespace. You can see this by examining the Get, Load and New functions in the generated unit. All three make calls to GetDocBinding, passing in TargetNamespace as the final parameter. TargetNamespace is a global constant string with the URI extracted from the schema or xml document you fed to the Binding Wizard.

    Because TargetNamespace is assigned to the root element as the default namespace no child elements will have a prefix.

    The way to do this:

    FDocumentName := 
      NewXMLDocument.GetDocBinding(
        'ns:DocumentName', // <-- Just add the prefix to the root node.
        TXMLDocumentName,
        TargetNamespace) as IXMLDocumentName;
    

    Now the root node will look like:

    
    

    And all child nodes will have the prefix when they are created.

提交回复
热议问题