none
XPath query on EDI document returning null RRS feed

  • Question

  • I am processing an inbound EDI 210 document through a custom receive pipeline component.  I need to pull a value from the document, do a database lookup, and then promote the return value for routing.  I'm having trouble with my XPath query, which seems to be returning null.  The code I have is:

    XmlDocument xdoc = new XmlDocument();
    xdoc.Load(originalStream);
    XmlNode root = xdoc.DocumentElement;
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
    nsmgr.AddNamespace("ns0", "http://schemas.microsoft.com/BizTalk/EDI/X12/2006");
    
    XmlNode N104 = xdoc.SelectSingleNode("//ns0:N1Loop1/ns0:N1[N103='25']/N104", nsmgr);
    
    DestinationID = GetDestinationID(N104.InnerText);
    
    pInMsg.Context.Promote("DestinationID", "http://ABSG.EDI.210.PropertySchema", DestinationID);
    


    My input xml is:

    <ns0:X12_00403_210 xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006">
    <ST>
    <ST01>210</ST01>
    <ST02>000000269</ST02>
    </ST>
    <ns0:B3>
    <B302>000000A0240Y061</B302>
    <B304>PP</B304>
    <B306>20110205</B306>
    <B307>84011</B307>
    <B311>A1B2</B311>
    <B312>20110208</B312>
    </ns0:B3>
    <ns0:N1Loop1>
    <ns0:N1>
    <N101>BT</N101>
    <N102>ABCD</N102>
    </ns0:N1>
    <ns0:N3>
    <N301>345 INTERNATIONAL DR RM 400</N301>
    </ns0:N3>
    <ns0:N4>
    <N401>BROOKS</N401>
    <N402>KY</N402>
    <N403>401095291</N403>
    <N404>US</N404>
    </ns0:N4>
    </ns0:N1Loop1>
    <ns0:N1Loop1>
    <ns0:N1>
    <N101>SH</N101>
    <N102>ABCD</N102>
    <N103>25</N103>
    <N104>0000V301F1</N104>
    </ns0:N1>
    <ns0:N3>
    <N301>345 INTERNATIONAL DR RM 400</N301>
    </ns0:N3>
    <ns0:N4>
    <N401>BROOKS</N401>
    <N402>KY</N402>
    <N403>401095291</N403>
    <N404>US</N404>
    </ns0:N4>
    </ns0:N1Loop1>
    


    Any ideas?

    Wednesday, September 7, 2011 7:07 PM

Answers

  • Two things. First it almost looks like you are using a promoted property. Not sure though, but I would grab the path from the node with something like this....

    N104 = xpath(XmlDocument,"string'your path here no quotes'/text())");  where XmlDocument  is the inbound xml.

    Also use this code below to troubleshoot. It will put the value on teh event viewer.

    System.Diagnostics.EventLog.WriteEntry("N104 ", N104 );


    Please Indicate "Mark as Answer" if this Post has Answered the Question
    • Marked as answer by FrankD302 Thursday, September 8, 2011 4:12 PM
    Wednesday, September 7, 2011 7:59 PM

All replies

  • Two things. First it almost looks like you are using a promoted property. Not sure though, but I would grab the path from the node with something like this....

    N104 = xpath(XmlDocument,"string'your path here no quotes'/text())");  where XmlDocument  is the inbound xml.

    Also use this code below to troubleshoot. It will put the value on teh event viewer.

    System.Diagnostics.EventLog.WriteEntry("N104 ", N104 );


    Please Indicate "Mark as Answer" if this Post has Answered the Question
    • Marked as answer by FrankD302 Thursday, September 8, 2011 4:12 PM
    Wednesday, September 7, 2011 7:59 PM
  • Thanks Carlos.  Using XPath navigator, I can now see my N104 value and my database lookup value when I write them to the EventLog.  Everything seems to be working within the component, but it still fails with "Object reference not set to an instance of an object."  I even put an EventLog message at the end of the IBaseMessage Execute method, just before it returns the message.  I get the EventLog message but then the error.  It seems as if there is a problem returning the message from IBaseMessage Execute.
    public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
            {
                IBaseMessagePart bodyPart = pInMsg.BodyPart;
                
                if (bodyPart != null)
                {
                    Stream originalStream = bodyPart.GetOriginalDataStream();
                    if (originalStream != null)
                    {
                        XmlDocument xdoc = new XmlDocument();
                        xdoc.Load(originalStream);
    
                        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
                        nsmgr.AddNamespace("ns0", "http://schemas.microsoft.com/BizTalk/EDI/X12/2006");                   
    
                        XPathNavigator xnav = xdoc.CreateNavigator();
                        XPathNavigator N104node = xnav.SelectSingleNode("//ns0:X12_00403_210/ns0:N1Loop1/ns0:N1[N103='25']/N104", nsmgr);
    
                        System.Diagnostics.EventLog.WriteEntry("N104", N104node.ToString());                    
    
                        DestinationID = GetDestinationID(N104node.ToString());
    
                        System.Diagnostics.EventLog.WriteEntry("DestinationID", DestinationID);  
    
                        if (DestinationID == "ICS")
                        {
                            pInMsg.Context.Promote("DestinationID", "http://ABSG.EDI.210.PropertySchema", DestinationID);
                        }                  
                            
                        originalStream.Position = 0;
    
                    }
                }
                System.Diagnostics.EventLog.WriteEntry("Finished", "Finished Processing"); 
                return pInMsg;
    
            }
    

    Wednesday, September 7, 2011 9:33 PM
  • Hi Frank,

    Before returning pInMsg assign original stream(if you didn't modify it) or new stream.

    pInMsg.BodyPart.Data = stream;


    Thanks, Angela
    Wednesday, September 7, 2011 10:06 PM
  • Thanks Angela--I tried that (pInMsg.BodyPart.Data = originalStream;) but it didn't work.  I'm still getting "Object reference not set to an instance of an object" even though everything in the component appears to be working.  I put "return pInMsg;" in a try-catch block but did not catch an exception.  Not sure what's going on.
    Wednesday, September 7, 2011 10:25 PM
  • Since the XPath Query is working I'll start a new thread for the remaining issue.

    Thursday, September 8, 2011 4:12 PM