Example #1
0
        public void PrependWithAttrWithAnotherOwnerDocumentThrows()
        {
            XmlDocument            doc = CreateDocumentWithElement();
            XmlAttribute           anotherDocumentAttr = new XmlDocument().CreateAttribute("attr");
            XmlAttributeCollection target = doc.DocumentElement.Attributes;

            Assert.Throws <ArgumentException>(() => target.Prepend(anotherDocumentAttr));
        }
Example #2
0
        public void PrependReturnsAddedAttribute()
        {
            XmlDocument doc     = CreateDocumentWithElement();
            XmlElement  element = doc.DocumentElement;
            var         newAttr = doc.CreateAttribute("attr");

            XmlAttributeCollection target = element.Attributes;
            var result = target.Prepend(newAttr);

            Assert.Same(newAttr, result);
        }
Example #3
0
        public void PrependInsertsAtPosition0()
        {
            const string attributeName = "newAttr";
            XmlDocument  doc           = CreateDocumentWithElement();
            XmlElement   element       = doc.DocumentElement;

            element.Attributes.Append(doc.CreateAttribute("anotherAttribute"));

            var newAttr = doc.CreateAttribute(attributeName);
            XmlAttributeCollection target = element.Attributes;

            target.Prepend(newAttr);

            Assert.Same(newAttr, target[0]);
        }
Example #4
0
        public void PrependDetachesAttrFromCurrentOwnerElement()
        {
            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");
            XmlAttributeCollection target             = destinationElement.Attributes;

            target.Prepend(attr);

            Assert.Same(destinationElement, attr.OwnerElement);
            Assert.False(element.HasAttribute(attributeName));
        }
Example #5
0
        public void PrependRemovesExistingAttribute()
        {
            const string attributeName = "existingAttr";
            const string attributeUri  = "existingUri";
            XmlDocument  doc           = CreateDocumentWithElement();
            XmlElement   element       = doc.DocumentElement;
            XmlAttribute anotherAttr   = element.Attributes.Append(doc.CreateAttribute("anotherAttribute"));
            XmlAttribute existingAttr  = element.Attributes.Append(doc.CreateAttribute(attributeName, attributeUri));

            // assert on implicitly set preconditions
            Assert.Same(anotherAttr, element.Attributes[0]);

            var newAttr = doc.CreateAttribute(attributeName, attributeUri);
            XmlAttributeCollection target = element.Attributes;

            target.Prepend(newAttr);

            Assert.Equal(2, target.Count);
            Assert.Same(newAttr, target[0]);
            Assert.Same(anotherAttr, target[1]);
        }
Example #6
0
    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.Prepend(newAttr);

        Console.WriteLine("Display the modified XML...\r\n");
        Console.WriteLine(doc.OuterXml);
    }
Example #7
0
        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");
        }