System.Xml.XmlAttributeCollection.InsertBefore C# (CSharp) Méthode

InsertBefore() public méthode

public InsertBefore ( XmlAttribute newNode, XmlAttribute refNode ) : XmlAttribute
newNode XmlAttribute
refNode XmlAttribute
Résultat XmlAttribute
        public XmlAttribute InsertBefore( XmlAttribute newNode, XmlAttribute refNode ) {
            if ( newNode == refNode )
                return newNode;

            if (refNode == null)
                return Append(newNode);

            if (refNode.OwnerElement != parent)
                throw new ArgumentException(Res.GetString(Res.Xdom_AttrCol_Insert));

            if (newNode.OwnerDocument != null && newNode.OwnerDocument != parent.OwnerDocument)
                throw new ArgumentException(Res.GetString(Res.Xdom_NamedNode_Context));

            if (newNode.OwnerElement != null)
                Detach( newNode );

            int offset = FindNodeOffset( refNode.LocalName, refNode.NamespaceURI );
            Debug.Assert( offset != -1 ); // the if statement above guarantees that the ref node is in the collection

            int dupoff = RemoveDuplicateAttribute( newNode );
            if ( dupoff >= 0 && dupoff < offset )
                offset--;
            InsertNodeAt( offset, newNode );

            return newNode;
        }

Usage Example

Exemple #1
0
		/// <summary>
		/// Sorts an attributes collection alphabeticlly.
		/// Uses bubble sort.
		/// </summary>
		/// <param name="a_toSort">The attribute collection to sort.</param>
		public static void SortAttributes(XmlAttributeCollection a_toSort)
		{
			if (a_toSort == null)
			{
				return;
			}

			bool l_change = true;
			while (l_change)
			{
				l_change = false;
				for (int i=1; i<a_toSort.Count; i++)
				{
					if (String.Compare(a_toSort[i].Name, a_toSort[i-1].Name, true) < 0)
					{
						//Replace
						a_toSort.InsertBefore(a_toSort[i], a_toSort[i-1]);
						l_change = true;
					}
				}
			}
			
		}
All Usage Examples Of System.Xml.XmlAttributeCollection::InsertBefore