Insert XML Node Using Defined Order - CSharp System.Xml

CSharp examples for System.Xml:XML Node

Description

Insert XML Node Using Defined Order

Demo Code


using System.Xml;
using System.Collections.Generic;
using System;/*from  w w w .ja va  2s .c  o  m*/

public class Main{
        public static void InsertNodeUsingDefinedOrder(XmlNode parent, XmlNode newChild, IComparer<XmlNode> nodeOrderComparer)
      {
         if (newChild.NodeType == XmlNodeType.Attribute)
         {
            XmlAttribute insertAfterNode = null;
            foreach (XmlAttribute childNode in parent.Attributes)
            {
               if (nodeOrderComparer.Compare(newChild, childNode) < 0)
               {
                  break;
               }
               insertAfterNode = childNode;
            }
            parent.Attributes.InsertAfter((XmlAttribute)newChild, insertAfterNode);
         }
         else
         {
            XmlNode insertAfterNode = null;
            foreach (XmlNode childNode in parent.ChildNodes)
            {
               if (nodeOrderComparer.Compare(newChild, childNode) < 0)
               {
                  break;
               }
               insertAfterNode = childNode;
            }
            parent.InsertAfter(newChild, insertAfterNode);
         }
      }
}

Related Tutorials