Search code examples
biztalkbiztalk-orchestrations

BizTalk getting "invalid token" when try to run Orchestration with the expression below


I have a task that involves using an Orchestration to de-batch a multi-record XML file and then sort it based on one field's value. The first expression outside the loop gets the record count:

recordCount = System.Convert.ToInt32(xpath(CustFile,("count/*[local-name()='Root' and namespace-uri()='']/*[local-name()='People' and namespace-uri()='']/*[local-name()='Customer' and namespace-uri()='']")));

counter = 0;

recordNumber = 0;

Next Expression inside the loop sets the Xpath value:

sXPath = System.String.Format("/*[local-name()='Root' and namespace-uri()='']/*[local-name()='People' and namespace-uri()='']/*[local-name()='Customer' and namespace-uri()='']", recordNumber);

The Next Expression defines the final message:

InternalCust = xpath(CustFile,sXPath);

The final expression increments the record counter for the loop to back and start again with the next record:

counter = counter + 1;

I think I can manage the sorting of the output message, but when I try to run it as is I get the following error in the console:

xlang/s engine event log entry: Uncaught exception (see the 'inner exception' below) has suspended an instance of service 'BizTalk_SelfStudy_Week_4_Project.BizTalk_Orchestration1(ae65e0c4-9db7-6f19-1e08-6f4fbe08affe)'. The service instance will remain suspended until administratively resumed or terminated. If resumed the instance will continue from its last persisted state and may re-throw the same unexpected exception.

InstanceId: 4a2d7256-4882-4853-8f7c-6e6054e78c4c

Shape name: Debatch Message

ShapeId: 6ee14c8d-e55b-408b-be63-e5d83fa412a6

Exception thrown from: segment 1, progress 19

Inner exception: The part 'part' of message 'InternalCust' contained a null value at the end of the construct block.

Exception type: NullPartException Source: Microsoft.XLANGs.Engine Target Site: Void ConstructionCompleteEvent(Boolean) The following is a stack trace that identifies the location where the exception occured

at Microsoft.XLANGs.Core.Part.ConstructionCompleteEvent(Boolean fKillUnderlyingPart) at Microsoft.XLANGs.Core.XMessage.ConstructionCompleteEvent(Boolean killUnderlyingPartWhenDirty) at BizTalk_SelfStudy_Week_4_Project.BizTalk_Orchestration1.segment1(StopConditions stopOn) at Microsoft.XLANGs.Core.SegmentScheduler.RunASegment(Segment s, StopConditions stopCond, Exception& exp)

I am at a loss, as I've tried to validate the xpath and all I get is the invalid token message on the validator. Ideas anyone?


Solution

  • As Johns-305 has pointed out, your construction of the XPath is wrong

    sXPath = System.String.Format("/*[local-name()='Root' and namespace-uri()='']/*[local-name()='People' and namespace-uri()='']/*[local-name()='Customer' and namespace-uri()='']", recordNumber);
    

    It is missing a placeholder e.g. '{0}' which it would substitute the recordNumber into.

    It probably should look like the below, which tells it which instance of Customer to select.

    sXPath = System.String.Format("/*[local-name()='Root' and namespace-uri()='']/*[local-name()='People' and namespace-uri()='']/*[local-name()='Customer' and namespace-uri()=''][{0}]", recordNumber);
    

    It also helps to Debug an Orchestration which would have allowed you to see that the XPath didn't include the Record Number and to test the Xpaths produced, a tool useful for this is Dan Sharps XML Viewer