protected virtual void InsertAndOrderChild(XElement parent, XElement child, ElementPosition parentPosition)
        {
            if (parentPosition == null ||
                parent.Descendants().Count() == 0)
            {
                parent.Add(child);
                return;
            }

            var childPosition = parentPosition.ChildrenOrder.IndexOf(
                parentPosition.ChildrenOrder.FirstOrDefault(x => x.Name == child.Name));

            if (childPosition == -1)
            {
                parent.Add(child);
                return;
            }

            var insertAfter = FindSiblingToInsertAfter(parent, parentPosition, childPosition);

            if (insertAfter == null)
            {
                AddSameChildrenInOrder(parent, child);
            }
            else
            {
                insertAfter.AddAfterSelf(child);
            }
        }
 public void Constructor_with_mixed_string_and_ElementPosition_array_children()
 {
     var element = new ElementPosition("ParentRef", new ElementPosition("ListID"), "FullName");
     Assert.AreEqual(2, element.ChildrenOrder.Count, "Count");
     Assert.AreEqual("ListID", element.ChildrenOrder[0].Name);
     Assert.AreEqual("FullName", element.ChildrenOrder[1].Name);
 }
Beispiel #3
0
 /// <summary>
 /// Adds or updates an XElement message
 /// </summary>
 /// <param name="parent">Parent XML Element that contains the whole message</param>
 /// <param name="parentElementPosition">ElementPosition information</param>
 /// <param name="message">Message to add</param>
 protected virtual void AddUpdateMessage(XElement parent, ElementPosition parentElementPosition, params object[] message)
 {
     if (message.Last() == null)
     {
         return;
     }
     _xmlBase.AddUpdateXElement(ConvertObjectArrayToXElement(message.ToList()), parent, false, parentElementPosition);
 }
 private XElement FindSiblingToInsertAfter(XElement parent, ElementPosition parentPosition, int childPosition)
 {
     for (int i = childPosition - 1; i >= 0; i--)
     {
         if (parent.Element(parentPosition.ChildrenOrder[i].Name) != null)
         {
             return(parent.Elements(parentPosition.ChildrenOrder[i].Name).Last());
         }
     }
     return(null);
 }
        /// <summary>
        /// Adds or updates a child XElement to the parent XElement argument
        /// </summary>
        /// <param name="newXElement">Child XElement to add/update</param>
        /// <param name="parent">XElement to add the newXElement to</param>
        /// <param name="allowDuplicates">If true, old child XElements are not overwritten</param>
        public virtual void AddUpdateXElement(XElement newXElement, XElement parent, bool allowDuplicates, ElementPosition parentElementPosition)
        {
            var firstGeneration = from child in parent.Descendants()
                                  where child.Parent.Name == parent.Name
                                  select child;

            var match = (from child in firstGeneration
                         where child.Name == newXElement.Name
                         select child).FirstOrDefault();

            if (match == null)
                InsertAndOrderChild(parent, newXElement, parentElementPosition);
            else
                if (newXElement.Descendants().Count() == 0)
                    if (allowDuplicates)
                        InsertAndOrderChild(parent, newXElement, parentElementPosition);
                    else
                        match.SetValue(newXElement.Value);
                else
                    AddUpdateXElement(newXElement.Descendants().First(), 
                        match, 
                        allowDuplicates, 
                        parentElementPosition.ChildrenOrder.FirstOrDefault(x => x.Name == newXElement.Name));
        }
 public virtual void InsertXElement(XElement parent, XElement newXElement, ElementPosition parentPosition)
 {
     InsertAndOrderChild(parent, newXElement, parentPosition);
 }
        /// <summary>
        /// Adds or updates a child XElement to the parent XElement argument
        /// </summary>
        /// <param name="newXElement">Child XElement to add/update</param>
        /// <param name="parent">XElement to add the newXElement to</param>
        /// <param name="allowDuplicates">If true, old child XElements are not overwritten</param>
        public virtual void AddUpdateXElement(XElement newXElement, XElement parent, bool allowDuplicates, ElementPosition parentElementPosition)
        {
            var firstGeneration = from child in parent.Descendants()
                                  where child.Parent.Name == parent.Name
                                  select child;

            var match = (from child in firstGeneration
                         where child.Name == newXElement.Name
                         select child).FirstOrDefault();

            if (match == null)
            {
                InsertAndOrderChild(parent, newXElement, parentElementPosition);
            }
            else
            if (newXElement.Descendants().Count() == 0)
            {
                if (allowDuplicates)
                {
                    InsertAndOrderChild(parent, newXElement, parentElementPosition);
                }
                else
                {
                    match.SetValue(newXElement.Value);
                }
            }
            else
            {
                AddUpdateXElement(newXElement.Descendants().First(),
                                  match,
                                  allowDuplicates,
                                  parentElementPosition.ChildrenOrder.FirstOrDefault(x => x.Name == newXElement.Name));
            }
        }
 public XElementBase(string xmlRootElementName)
 {
     _xmlRootElementName = xmlRootElementName;
     ElementOrder        = new ElementPosition(_xmlRootElementName);
     ResetXml();
 }
        protected virtual void InsertAndOrderChild(XElement parent, XElement child, ElementPosition parentPosition)
        {
            if (parentPosition == null ||
                parent.Descendants().Count() == 0)
            {
                parent.Add(child);
                return;
            }

            var childPosition = parentPosition.ChildrenOrder.IndexOf(
                parentPosition.ChildrenOrder.FirstOrDefault(x => x.Name == child.Name));

            if (childPosition == -1)
            {
                parent.Add(child);
                return;
            }

            var insertAfter = FindSiblingToInsertAfter(parent, parentPosition, childPosition);

            if (insertAfter == null)
                AddSameChildrenInOrder(parent, child);
            else
                insertAfter.AddAfterSelf(child);
        }
 public virtual void InsertXElement(XElement parent, XElement newXElement, ElementPosition parentPosition)
 {
     InsertAndOrderChild(parent, newXElement, parentPosition);
 }
 public XElementBase(string xmlRootElementName)
 {
     _xmlRootElementName = xmlRootElementName;
     ElementOrder = new ElementPosition(_xmlRootElementName);
     ResetXml();
 }
 private XElement FindSiblingToInsertAfter(XElement parent, ElementPosition parentPosition, int childPosition)
 {
     for (int i = childPosition - 1; i >= 0; i--)
     {
         if (parent.Element(parentPosition.ChildrenOrder[i].Name) != null)
             return parent.Elements(parentPosition.ChildrenOrder[i].Name).Last();
     }
     return null;
 }