public void InsertBeforeReturnsInsertedAttr() { XmlDocument doc = CreateDocumentWithElement(); XmlElement element = doc.DocumentElement; XmlAttribute refAttr = element.Attributes.Append(doc.CreateAttribute("attr1", "some:uri1")); XmlAttribute newAttr = doc.CreateAttribute("attr2", "some:uri2"); XmlAttributeCollection target = element.Attributes; Assert.Same(newAttr, target.InsertBefore(newAttr, refAttr)); }
public void InsertBeforeWithRefAttrWithAnotherOwnerElementThrows() { XmlDocument doc = CreateDocumentWithElement(); XmlElement element = doc.DocumentElement; XmlAttribute newAttr = doc.CreateAttribute("newAttr"); XmlElement anotherElement = doc.CreateElement("anotherElement"); XmlAttribute anotherOwnerElementAttr = anotherElement.SetAttributeNode("anotherOwnerElementAttr", string.Empty); XmlAttributeCollection target = element.Attributes; Assert.Throws <ArgumentException>(() => target.InsertBefore(newAttr, anotherOwnerElementAttr)); }
public void InsertBeforeWithAttrWithAnotherOwnerDocumentThrows() { XmlDocument doc = CreateDocumentWithElement(); XmlElement element = doc.DocumentElement; XmlAttribute existingAttr = doc.CreateAttribute("existingAttr"); element.Attributes.Append(existingAttr); XmlAttribute anotherOwnerDocumentAttr = new XmlDocument().CreateAttribute("anotherOwnerDocumentAttr"); XmlAttributeCollection target = element.Attributes; Assert.Throws <ArgumentException>(() => target.InsertBefore(anotherOwnerDocumentAttr, existingAttr)); }
public void InsertBeforeAfterPrepend() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml("<root b2='amethyst' />"); XmlElement el = xmlDoc.DocumentElement; XmlAttributeCollection col = xmlDoc.DocumentElement.Attributes; XmlAttribute attr = xmlDoc.CreateAttribute("b1"); attr.Value = "garnet"; col.InsertAfter(attr, null); Assert.AreEqual("garnet", el.GetAttributeNode("b1").Value, "InsertAfterNull"); Assert.AreEqual(el.GetAttribute("b1"), col[0].Value, "InsertAfterNull.Pos"); attr = xmlDoc.CreateAttribute("b3"); attr.Value = "bloodstone"; col.InsertAfter(attr, el.GetAttributeNode("b2")); Assert.AreEqual("bloodstone", el.GetAttributeNode("b3").Value, "InsertAfterAttr"); Assert.AreEqual(el.GetAttribute("b3"), col[2].Value, "InsertAfterAttr.Pos"); attr = xmlDoc.CreateAttribute("b4"); attr.Value = "diamond"; col.InsertBefore(attr, null); Assert.AreEqual("diamond", el.GetAttributeNode("b4").Value, "InsertBeforeNull"); Assert.AreEqual(el.GetAttribute("b4"), col[3].Value, "InsertBeforeNull.Pos"); attr = xmlDoc.CreateAttribute("warning"); attr.Value = "mixed modern and traditional;-)"; col.InsertBefore(attr, el.GetAttributeNode("b1")); Assert.AreEqual("mixed modern and traditional;-)", el.GetAttributeNode("warning").Value, "InsertBeforeAttr"); Assert.AreEqual(el.GetAttributeNode("warning").Value, col[0].Value, "InsertBeforeAttr.Pos"); attr = xmlDoc.CreateAttribute("about"); attr.Value = "lists of birthstone."; col.Prepend(attr); Assert.AreEqual("lists of birthstone.", col[0].Value, "Prepend"); }
public void InsertBeforeCanInsertBeforeTheLast() { XmlDocument doc = CreateDocumentWithElement(); XmlElement element = doc.DocumentElement; element.Attributes.Append(doc.CreateAttribute("attr1", "some:uri1")); element.Attributes.Append(doc.CreateAttribute("attr2", "some:uri2")); XmlAttribute refAttr = element.Attributes.Append(doc.CreateAttribute("attr3", "some:uri3")); XmlAttribute newAttr = doc.CreateAttribute("newAttr"); XmlAttributeCollection target = element.Attributes; target.InsertBefore(newAttr, refAttr); Assert.Equal(4, target.Count); Assert.Same(newAttr, target[2]); Assert.Same(refAttr, target[3]); }
public void InsertBeforeWithNullRefAttrAddsToTheEnd() { XmlDocument doc = CreateDocumentWithElement(); XmlElement element = doc.DocumentElement; XmlAttribute attr1, attr2, attr3; attr1 = element.Attributes.Append(doc.CreateAttribute("attr1")); attr2 = element.Attributes.Append(doc.CreateAttribute("attr2")); attr3 = doc.CreateAttribute("attr3"); XmlAttributeCollection target = element.Attributes; target.InsertBefore(attr3, null); Assert.Equal(3, target.Count); Assert.Same(attr1, target[0]); Assert.Same(attr2, target[1]); Assert.Same(attr3, target[2]); }
public void InsertBeforeWithSameRefAttrKeepsOrderIntactAndReturnsTheArgument() { XmlDocument doc = CreateDocumentWithElement(); XmlElement element = doc.DocumentElement; XmlAttribute attr1, attr2, attr3; attr1 = element.Attributes.Append(doc.CreateAttribute("attr1")); attr2 = element.Attributes.Append(doc.CreateAttribute("attr2")); attr3 = element.Attributes.Append(doc.CreateAttribute("attr3")); XmlAttributeCollection target = element.Attributes; XmlAttribute result = target.InsertBefore(attr2, attr2); Assert.Equal(3, target.Count); Assert.Same(attr1, target[0]); Assert.Same(attr2, target[1]); Assert.Same(attr3, target[2]); Assert.Same(attr2, result); }
public void InsertBeforeDetachesAttrFromCurrentOwnerElement() { const string attributeName = "movingAttr"; XmlDocument doc = CreateDocumentWithElement(); XmlElement element = doc.DocumentElement; XmlAttribute attr = element.Attributes.Append(doc.CreateAttribute(attributeName)); // assert on implicitly set preconditions Assert.Same(element, attr.OwnerElement); Assert.True(element.HasAttribute(attributeName)); XmlElement destinationElement = doc.CreateElement("anotherElement"); XmlAttribute refAttr = destinationElement.Attributes.Append(doc.CreateAttribute("anotherAttr")); XmlAttributeCollection target = destinationElement.Attributes; target.InsertBefore(attr, refAttr); Assert.Same(destinationElement, attr.OwnerElement); Assert.False(element.HasAttribute(attributeName)); }
public void InsertBeforeRemovesDupRefAttr() { const string attributeName = "existingAttr"; const string attributeUri = "some:existingUri"; XmlDocument doc = CreateDocumentWithElement(); XmlElement element = doc.DocumentElement; XmlAttribute refAttr = element.Attributes.Append(doc.CreateAttribute(attributeName, attributeUri)); //dup XmlAttribute anotherAttr1 = element.Attributes.Append(doc.CreateAttribute("attr1", "some:uri1")); XmlAttribute anotherAttr2 = element.Attributes.Append(doc.CreateAttribute("attr2", "some:uri2")); XmlAttribute newAttr = doc.CreateAttribute(attributeName, attributeUri); XmlAttributeCollection target = element.Attributes; target.InsertBefore(newAttr, refAttr); Assert.Equal(3, target.Count); Assert.Same(newAttr, target[0]); Assert.Same(anotherAttr1, target[1]); Assert.Same(anotherAttr2, target[2]); }
public static void SortAttributes(XmlAttributeCollection attribCol) { if (attribCol == null) { return; } bool hasChanged = true; while (hasChanged) { hasChanged = false; for (int i = 1; i < attribCol.Count; i++) { if (String.Compare(attribCol[i].Name, attribCol[i - 1].Name, true) < 0) { attribCol.InsertBefore(attribCol[i], attribCol[i - 1]); hasChanged = true; } } } }
public static void Main() { XmlDocument doc = new XmlDocument(); doc.LoadXml("<book ISBN='1-861001-57-5'>" + "<title>Pride And Prejudice</title>" + "</book>"); //Create a new attribute. XmlAttribute newAttr = doc.CreateAttribute("genre"); newAttr.Value = "novel"; //Create an attribute collection and add the new attribute //to the collection. XmlAttributeCollection attrColl = doc.DocumentElement.Attributes; attrColl.InsertBefore(newAttr, attrColl[0]); Console.WriteLine("Display the modified XML...\r\n"); Console.WriteLine(doc.OuterXml); }
/// <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; } } } }