/// <summary>
        /// Adds annotation to the payload element.
        /// </summary>
        /// <typeparam name="T">The type of the payload element to work with.</typeparam>
        /// <param name="payloadElement">The payload element to add the annotation to.</param>
        /// <param name="annotation">The annotation to add.</param>
        /// <returns>The <paramref name="payloadElement"/> after the annotation was added.</returns>
        public static T AddAnnotation <T>(this T payloadElement, ODataPayloadElementAnnotation annotation) where T : ODataPayloadElement
        {
            ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");

            payloadElement.Annotations.Add(annotation);
            return(payloadElement);
        }
        /// <summary>
        /// Remove all existing annotations of type <typeparamref name="T"/> and set <paramref name="annotation"/>.
        /// </summary>
        /// <typeparam name="T">The type of the annotation to set.</typeparam>
        /// <param name="payloadElement">The payload element to set the annotation on.</param>
        /// <param name="annotation">The annotation to set.</param>
        /// <returns>The <paramref name="payloadElement"/> with the <paramref name="annotation"/> set.</returns>
        public static T SetAnnotation <T>(this T payloadElement, ODataPayloadElementAnnotation annotation) where T : ODataPayloadElement
        {
            ExceptionUtilities.CheckArgumentNotNull(annotation, "annotation");

            payloadElement.RemoveAnnotations(annotation.GetType());
            payloadElement.AddAnnotation(annotation);
            return(payloadElement);
        }
 /// <summary>
 /// Asserts that two sets of annotaitons are the same (incl. the same order)
 /// </summary>
 /// <param name="expected">The expected set of annotations</param>
 /// <param name="observed">The actual set of annotations</param>
 private void Compare(IList <ODataPayloadElementAnnotation> expectedAnnotations, IList <ODataPayloadElementAnnotation> observedAnnotations)
 {
     for (int i = 0; i < expectedAnnotations.Count; i++)
     {
         ODataPayloadElementAnnotation expectedAnnotation = expectedAnnotations[i];
         ODataPayloadElementAnnotation matchingAnnotation = null;
         string errorMessage = null;
         matchingAnnotation = observedAnnotations.Where(ann => CompareAnnotation(expectedAnnotation, ann, out errorMessage)).SingleOrDefault();
         if (matchingAnnotation == null)
         {
             this.Assert.Fail("For annotation " + expectedAnnotation.StringRepresentation + " no matching observed annotation was found." +
                              "Compare with expected annotations failed in: " + errorMessage);
         }
         else
         {
             observedAnnotations.Remove(matchingAnnotation);
         }
     }
 }
        private bool CompareAnnotation(ODataPayloadElementAnnotation expectedAnnotation, ODataPayloadElementAnnotation observedAnnotation, out string errorMessage)
        {
            try
            {
                errorMessage = null;
                this.Assert.AreEqual(expectedAnnotation.GetType(), observedAnnotation.GetType(), "Annotation types should be same");
                if (expectedAnnotation is XmlTreeAnnotation)
                {
                    XmlTreeAnnotation e = expectedAnnotation as XmlTreeAnnotation;
                    XmlTreeAnnotation o = observedAnnotation as XmlTreeAnnotation;
                    this.Assert.IsNotNull(o, "Observed annotation cannot be null");
                    this.Assert.AreEqual(e.LocalName, o.LocalName, "Local names should match");
                    this.Assert.AreEqual(e.NamespaceName, o.NamespaceName, "Namespace names should be equal");
                    this.Assert.AreEqual(e.PropertyValue, o.PropertyValue, "Property values must be equal");
                    this.Assert.AreEqual(e.IsAttribute, o.IsAttribute, "IsAttribute values should be equal");
                    this.Assert.AreEqual(e.Children.Count, o.Children.Count, "Children count must be equal");
                    Compare(e.Children.Cast <ODataPayloadElementAnnotation>().ToList(), o.Children.Cast <ODataPayloadElementAnnotation>().ToList());
                }
                else if (expectedAnnotation is SelfLinkAnnotation)
                {
                    SelfLinkAnnotation e = expectedAnnotation as SelfLinkAnnotation;
                    SelfLinkAnnotation o = observedAnnotation as SelfLinkAnnotation;
                    this.Assert.IsNotNull(o, "Observed annotation cannot be null");
                    this.Assert.AreEqual(e.Value, o.Value, "Values should match");
                }
                else if (expectedAnnotation is ContentTypeAnnotation)
                {
                    ContentTypeAnnotation e = expectedAnnotation as ContentTypeAnnotation;
                    ContentTypeAnnotation o = observedAnnotation as ContentTypeAnnotation;
                    this.Assert.IsNotNull(o, "Observed annotation cannot be null");
                    this.Assert.AreEqual(e.Value, o.Value, "Values should match");
                }

                return(true);
            }
            catch (DataComparisonException e)
            {
                errorMessage = "Message: " + e.Message + " Exception type:" + e.GetType() + " Stack:" + e.StackTrace;
                return(false);
            }
        }