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?