Search code examples
delphixml-binding

How to create "New" functions using Delphi XSD databinding wizard


According to Embarcadero's documentation

Call the generated New... function to create the TXMLDocument instance for an empty document when you want to create all the data in your application:

var
  StockList: IXMLStockListType;
begin
  StockList := Newstocklist;     

_di_IStockListType StockList = NewStockListType();

However, I don't get any New... functions in the 26833 lines of code Delphi generated when importing the XSD for the Clinical Document Architecture .

So, what triggers the generation of these New functions?


Solution

  • If your XSD document contains multiple xs:element (or simpletype,complextype,...) nodes at the root level, the wizard can't determine what the root level element will eventually be of your xml document. In that case you must write the Newxxx statement yourself:

    var yourType : IXMLYourType;
    ...
    yourType := NewXMLDocument.GetDocBinding(
                  '<root level yourtype tagname>', 
                  TXMLYourType, 
                  '') as IXMLYourType;
    

    Try to import an xml document that adheres to the xsd and you will see that the newxxx function will be generated.

    UPDATE

    screenshot

    If you check the documentElementType for the Type you want to use as root element, the new, load and get functions are created. If you don't do that you will get this warning:

    warning