0

Problem: I need to check whether an incoming document, inside an XML element, is XML or Edifact formatted. Depending on what format the document has, it needs to be processed accordingly.

Current solution: An XDocument instance is created from the incoming message. The incoming message is always XML.

var originalStream = pInMsg.BodyPart.GetOriginalDataStream();
                        XDocument xDoc;
                        using (XmlReader reader = XmlReader.Create(originalStream))
                        {
                            reader.MoveToContent();
                            xDoc = XDocument.Load(reader);
                        }

After this the document is extracted from the XML element "msgbody". Currently it assumes this to be XML formatted, which throws an error when the document is Edifact formatted. The code below extracts it, and the creates a new XDocument, which is sent to the MessageBox.

string extractedDocument = xDoc.Root.Element("msgbody").Value;
extractedDocument = HttpUtility.HtmlDecode(extractedDocument);
XDocument outputXml = XDocument.Parse(extractedDocument);

Example message from biztalk:

<NewTable>
  <conversationID>2ff845e7-30a4-482e-98d6-8c3249c5dea1</conversationID>
  <hostUTC>2018-12-17T12:17:04.107Z</hostUTC>
  <msgType>INVOIC</msgType>
  <msgid>721254</msgid>
  <icref>36655</icref>
  <msgFormat_org>EDIFACTBauhaus</msgFormat_org>
  <msgFormat>EDI</msgFormat>
  <msgbody>"Edifact or XML document"</msgbody>
  <fromID>GLN:5790034516518</fromID>
  <toID>GLN:5790000451485</toID>
</NewTable>

Question: How can I create a check for the document inside the msgbody tag, to determine whether it is XML or Edifact formatted, before processing it?

Leth
  • 1,033
  • 3
  • 15
  • 40
  • The right side of following webpage show many type formats : https://www.edi-plus.com/resources/message-formats/edifact/ – jdweng Dec 20 '18 at 11:29
  • What you post is XML with the tag msgFormat_org equals EDIFACTBauhaus – jdweng Dec 20 '18 at 11:31
  • Yes, the message from Biztalk is always XML. However, I am extracting the document inside the msgbody tag, which can contain either XML or Edifact. I need a way to determine whether this document is XML or Edifact. – Leth Dec 20 '18 at 11:34
  • The data inside the msgFormat and msgFormat_org is not consistent, and may not contain the keyword 'Edifact'. – Leth Dec 20 '18 at 11:38
  • I must point out again, you should never use XDocument or XmlDocument in a BizTalk Pipeline. – Johns-305 Dec 20 '18 at 17:53
  • Sorry, your question isn't too clear (title and last question conflict). You are always receiving the Xml Document and only the content of can change? Or, is the full content either Xml or EDIFACT? – Johns-305 Dec 20 '18 at 17:57
  • @Johns-305 He is getting an XML payload, where one node contains either a XML payload or a EDIFACT one – Dijkgraaf Dec 25 '18 at 02:11
  • What adapter is this payload coming in via? – Dijkgraaf Dec 25 '18 at 02:13

1 Answers1

0

I like using a dictionary to get all the properties using xml linq. See code below. If you are getting string response the nuse instead of the Load(filename) method use Parse(string).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication93
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {

            XDocument doc = XDocument.Load(FILENAME);
            Dictionary<string, string> dict = doc.Descendants("NewTable").Elements()
                .GroupBy(x => x.Name.LocalName, y => (string)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }

    }


}
jdweng
  • 33,250
  • 2
  • 15
  • 20