XMLPort - Accessing child elements dynamically

I have an XMLPort (import). One of the elements is a Record, and there's several Elements underneath that line up with the fields (ie: Order Header::"Bill to Name")

Each element also has sub elements that contain information about how the XML Port should behave, which are supplied dynamically by the sender of the XML. Example:

zuy7iyao1fg6.png

I'm trying to figure out how to dynamically access those sub-elements. My first thought was in marketplace_order::OnBeforeInsertRecord, I might be able to access them. However, the only way I've been able to do this so far is to assign the value to a string DataSource (such as Additional_info1 in the screenshot).

This isn't really feasible, since there are hundreds of nodes, and I need to do them dynamically.

Ideally, there'd be something like

"for each element directly under marketplace_info ...... find node with name "error" ..... if that element has a value, stop XML port and throw up a message with value"

...


The only other thing I can think of is for every single elemtn (document_type, no in the example)-- in OnAfterAssignField or OnAfterAssignVariable , call a function-- "DoCommonThing()"-- it would require pasting DoCommonThing into every OnAfterAssignField / OnAfterAssignVariable

It also occurs to me that an XMLPort, which structurally sound, might not be the right tool for what I'm looking to do?



Comments

  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    I think the XMLPort is not the best choice to do that.

    I would try to pre-load the file in OnPreXMLport into a DotNET XML Variable (or XMLDOMDocument object if it is supposed to work in Classic Client, then use a XPath search expression to find a list of nodes including some specific text (error?), or element names, if found then throw an error, or iterate through returned node list and do whatever else needs to be done.

    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • halcyon1234halcyon1234 Member Posts: 8
    Slawek: I'm going to go that route. In OnPreXMLPort, would you suggest I use the XMLDoc to .Load(currXMLport.FILENAME), or is there a better / safer way to access the XML Document that the port is using?
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    I could possibly see two other ways:

    One way is like you suggested - to include a bit of the code on every node in OnAfterAssignField or OnAfterAssignVariable, call some function on it checking for errors.


    Another way would be to build a custom buffer table where additional field/error handling data could be stored, base the XMLport on that table, and the in OnInsert open make a copy of the record into a Record Ref variable (using REcref.GETTABLE(buffer) ) and then loop through its fields looking for error codes in there.

    If you build the buffer in some specific way, like keeping additional information fields next to each other you could do simple FOR based loop iterating through fields of your interest, and after successful check do a TRANSFERFIELDS to your real destination.


    XPath based search is nice as you don't have to worry that much about the structure - you can build your XPath search expression to find nodes of your interest in any part of the XML structure.

    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • krikikriki Member, Moderator Posts: 9,115
    [Topic moved from 'NAV Tips & Tricks' forum to 'NAV Three Tier' forum]

    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Sign In or Register to comment.