We had a case where many maps need to convert YYYYMMDD date format to a standard XML date.


        /// <summary>
        /// Convert date in format YYYYMMDD to the standard XML Date format
        /// </summary>
        /// <param name="inDate"></param>
        /// <returns></returns>
        public static string ConvertYYYYMMDDtoXmlDate(string inDate)
        {
            try
            {
                return inDate.Substring(0, 4) + "-" +
                        inDate.Substring(4, 2) + "-" +
                        inDate.Substring(6, 2);
            }
            catch
            {
                return "invalid-YYYYMMDD-conversion from value=" + inDate + "'";
            }
        }

If you are getting this error:

The “succeeded” operator can only be applied to an immediate child scope or service,
then make sure you specify a valid transactionName in parentheses, for example:

!succeeded(SendLoanData)

Note: SendLoanData is not the name of the scope, it is the TransactionIdentifier that you assign to the scope (which you could then use the same text value as the scope name).

This error was related to the compiler error:
“must receive before sending a fault message on an implemented port”.

The issue here has to do with publishing an orchestration as a web service.

This has already been discussed in the two blogs below
(more references for more info about the “succeeded” operator in BizTalk).

http://www.traceofthought.net/CommentView,guid,abdd6ca2-e964-437d-8bee-0188ec5b5afa.aspx#commentstart

http://staff.southworks.net/blogs/lito/archive/2007/01/02/Biztalk-Server-2004_3A00_-The-_1C20_succeded_280029001D20_-operator.aspx

How to use TextWriter to file.


            string strOutFileName = @"c:YourDirYourFile.xml";
   ... assumes you have an xmlDoc somewhere
            System.Xml.XmlTextWriter tw = new XmlTextWriter(strOutFileName, System.Text.Encoding.ASCII);
            xmlDoc.WriteTo(tw);
            //xmlTypedDoc.Document.WriteTo(tw);
            tw.Close();

How to add an node to an XML Document (via a C# helper that can be called in an expression shape from an orchestration).



First example (uses AppendChild):


<code>
        public static XmlDocument AddExceptionNodeToTMXML(XmlDocument TMXMLDocIn, Exception ex)
        {
            // three import fields in System.Exception are ex.Message, ex.StackTrace, ex.Source
            // then there is an Ex.InnerException

            XmlDocument xmlDocNewTMXML = new XmlDocument();
            xmlDocNewTMXML.LoadXml(TMXMLDocIn.OuterXml);  // make sure we don't update the original TMXMLDocIn

            string errMessage = "";
            string errSource = "";
            string errStackTrace = "";
            string xml;

            if (ex.InnerException == null)
            {
                // if not nested exceptions, do not number them.
                errMessage = ex.Message;
                errStackTrace = ex.StackTrace;
                errSource = ex.Source;
            }
            else
            {
                // loop through all the nested exceptions and concatenate them into single long string variables
                System.Exception innerEx = ex;
                int loopCounter = 0;
                while (innerEx != null)
                {
                    // number the nested exceptions
                    loopCounter++;
                    string separator = "";
                    if (loopCounter &gt; 1)
                        separator = ";";

                    errMessage = errMessage + separator + "n     Ex:" + loopCounter + ") " + innerEx.Message;
                    errStackTrace = errStackTrace + separator + "n     Ex:" + loopCounter + ") " + innerEx.StackTrace;
                    errSource = errSource + separator + "n     Ex:" + loopCounter + ") " + innerEx.Source;
                    innerEx = innerEx.InnerException;

                }

            }

            // Added CDATA clauses below, because the err fields may have
            // ampersand, less-than, greater than, or other problem characters that cause the xmlDoc.Load to  fail. 
            xml =
                    @"&lt;exs:Exceptions xmlns:exs='http://YourSchemaNamespace.Exceptions'&gt;" +
                    "&lt;ex:Exception index='1' id='1' type='' xmlns:ex='http://YourSchemaNamespace.Exception'&gt;" +
                    "  &lt;messageText&gt;&lt;![CDATA[" + errMessage + "]]&gt;&lt;/messageText&gt;" +
                    "  &lt;source&gt;&lt;![CDATA[" + errSource + "]]&gt;&lt;/source&gt;" +
                    "  &lt;stackTrace&gt;&lt;![CDATA[" + errStackTrace + "]]&gt;&lt;/stackTrace&gt;" +
                    "&lt;/ex:Exception&gt;" +
                    "&lt;/exs:Exceptions&gt;";


            XmlDocument exceptionXML = new XmlDocument();
            exceptionXML.LoadXml(xml);


            string strXPathRoot = "/*[local-name()='TMXML' and namespace-uri()='http://YourSchemaNamespace.MainSchema']";
            XmlNode nodeTMXML = xmlDocNewTMXML.SelectSingleNode(strXPathRoot);

            string strExXPathRoot = "/*[local-name()='Exceptions' and namespace-uri()='http://YourSchemaNamespace.Exceptions']";
            XmlNode nodeExceptions = exceptionXML.SelectSingleNode(strExXPathRoot);


            nodeTMXML.AppendChild(xmlDocNewTMXML.ImportNode(nodeExceptions, true));

            return xmlDocNewTMXML;

        }



</code>

Second example (uses CreateElement)


<code>

	// This can be called from a BusinessRule which passes a TypedXmlDocument
	// instead of a normal XmlDocument
	public static void AddItineraryStep(TypedXmlDocument txd, string stepName, string activityPolicy)
        {

                  // can we find the XPath  indicate d?
                  string strXPathRoot = "//*[local-name()='ItinerarySteps']";
                  XmlNode node = LocateXPath(txd, strXPathRoot);
                  if (null == node)
                  {
                      throw new Exception("AddItineraryStep: XPath did not return a valid node, XPath=" +
                          strXPathRoot + "  XML=" + txd.Document.OuterXml);
                  };


                  // determine the root node for the document
                  // if the XPath selects the TXD, it will have no root if it is an XmlDocument
                  // in that case, simply use the document from the TXD
                  XmlDocument root = node.OwnerDocument;
                  if (null == root)
                  {
                        // if the XPath selects the TXD, it may
                        // so fix accordingly
                        root = txd.Document as XmlDocument;
                        if (null == root) return;
                  }


                  // create a new node and add it in
                  string nodeName = "ItineraryStep";
                  XmlElement newNode = root.CreateElement(nodeName);
                  node.AppendChild(newNode);

                  // Not sure why ImportNode not available. Seems like it would be easier
                  //node.AppendChild(txd.Document.ImportNode(nodeItineraryStep, true));

                  //strXPathRoot = "/*[local-name()='Header']/*[local-name()='Itinerary']/*[local-name()='ItineraryStep']";
                  strXPathRoot = "//*[local-name()='ItineraryStep']";
                  node = LocateXPath(txd, strXPathRoot);
                  if (null == node)
                  {
                      throw new Exception("AddItineraryItem: XPath did not return a valid node, XPath=" +
                          strXPathRoot + "  XML=" + txd.Document.OuterXml);
                  };

                  XmlElement newSubNode;

                  nodeName = "name";
                  newSubNode = root.CreateElement(nodeName);
                  newSubNode.InnerText = stepName;
                  newNode.AppendChild(newSubNode);

                  nodeName = "status";
                  newSubNode = root.CreateElement(nodeName);
                  newSubNode.InnerText = "PENDING";
                  newNode.AppendChild(newSubNode);

                  nodeName = "errorType";
                  newSubNode = root.CreateElement(nodeName);
                  // leave value to empty-string (no need to set InnerText)
                  newNode.AppendChild(newSubNode);

                  nodeName = "activityPolicy";
                  newSubNode = root.CreateElement(nodeName);
                  newSubNode.InnerText = activityPolicy;
                  newNode.AppendChild(newSubNode);


        }

        internal static XmlNode LocateXPath(TypedXmlDocument txd, string xPath)
        {
            // does the TXD contain a node?
            XmlNode parent = txd.Document;
            if (null == parent) return null;

            // is there an XPath specified? if not, return the parent
            if ((xPath == null) || (xPath == string.Empty)) return parent;

            // return the first node that the XPath points to
            return parent.SelectSingleNode(xPath, txd.NamespaceManager);
        }

</code>