Пример #1
0
        public void InsertAfterWithSameRefAttrKeepsOrderIntactAndReturnsTheArgument()
        {
            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.InsertAfter(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);
        }
Пример #2
0
        public void InsertAfterCanInsertInTheMiddle()
        {
            XmlDocument doc     = CreateDocumentWithElement();
            XmlElement  element = doc.DocumentElement;

            element.Attributes.Append(doc.CreateAttribute("attr1", "some:uri1"));
            XmlAttribute refAttr = element.Attributes.Append(doc.CreateAttribute("attr2", "some:uri2"));

            element.Attributes.Append(doc.CreateAttribute("attr3", "some:uri3"));
            XmlAttribute newAttr = doc.CreateAttribute("newAttr");

            XmlAttributeCollection target = element.Attributes;

            target.InsertAfter(newAttr, refAttr);

            Assert.Equal(4, target.Count);
            Assert.Same(refAttr, target[1]);
            Assert.Same(newAttr, target[2]);
        }
Пример #3
0
        public void InsertAfterWithNullRefAttrAddsToTheBeginning()
        {
            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.InsertAfter(attr3, null);

            Assert.Equal(3, target.Count);
            Assert.Same(attr3, target[0]);
            Assert.Same(attr1, target[1]);
            Assert.Same(attr2, target[2]);
        }
Пример #4
0
        [SkipOnTargetFramework(~TargetFrameworkMonikers.NetFramework)] //Fix for this edge case was only made in NetCore
        public void InsertAfterThrowsArgumentOutOfRangeExceptionForDupRefAttrAtTheEnd_OldBehavior()
        {
            const string attributeName = "existingAttr";
            const string attributeUri  = "some:existingUri";
            XmlDocument  doc           = CreateDocumentWithElement();
            XmlElement   element       = doc.DocumentElement;
            XmlAttribute anotherAttr1  = element.Attributes.Append(doc.CreateAttribute("attr1", "some:uri1"));
            XmlAttribute anotherAttr2  = element.Attributes.Append(doc.CreateAttribute("attr2", "some:uri2"));
            XmlAttribute refAttr       = element.Attributes.Append(doc.CreateAttribute(attributeName, attributeUri)); //dup
            XmlAttribute newAttr       = doc.CreateAttribute(attributeName, attributeUri);

            XmlAttributeCollection target = element.Attributes;

            Assert.Throws <ArgumentOutOfRangeException> (() => target.InsertAfter(newAttr, refAttr));

            Assert.Equal(2, target.Count);
            Assert.Same(anotherAttr1, target[0]);
            Assert.Same(anotherAttr2, target[1]);
        }
Пример #5
0
        public void InsertAfterDetachesAttrFromCurrentOwnerElement()
        {
            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.InsertAfter(attr, refAttr);

            Assert.Same(destinationElement, attr.OwnerElement);
            Assert.False(element.HasAttribute(attributeName));
        }
Пример #6
0
        public void InsertAfterRemovesDupRefAttrAtTheEnd()
        {
            const string attributeName = "existingAttr";
            const string attributeUri  = "some:existingUri";
            XmlDocument  doc           = CreateDocumentWithElement();
            XmlElement   element       = doc.DocumentElement;
            XmlAttribute anotherAttr1  = element.Attributes.Append(doc.CreateAttribute("attr1", "some:uri1"));
            XmlAttribute anotherAttr2  = element.Attributes.Append(doc.CreateAttribute("attr2", "some:uri2"));
            XmlAttribute refAttr       = element.Attributes.Append(doc.CreateAttribute(attributeName, attributeUri)); //dup
            XmlAttribute newAttr       = doc.CreateAttribute(attributeName, attributeUri);

            XmlAttributeCollection target = element.Attributes;

            target.InsertAfter(newAttr, refAttr);

            Assert.Equal(3, target.Count);
            Assert.Same(anotherAttr1, target[0]);
            Assert.Same(anotherAttr2, target[1]);
            Assert.Same(newAttr, target[2]);
        }
Пример #7
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.InsertAfter(newAttr, attrColl[0]);

        Console.WriteLine("Display the modified XML...\r\n");
        Console.WriteLine(doc.OuterXml);
    }