Пример #1
0
        public void RemoveAtWithInvalidIndexReturnsNull()
        {
            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;

            Assert.Null(target.RemoveAt(3));
        }
Пример #2
0
        public void RemoveAtReturnsDeletedAttr()
        {
            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;
            var actualAttr = target.RemoveAt(1);

            Assert.Same(attr2, actualAttr);
        }
Пример #3
0
    public static void Main()
    {
        XmlDocument doc = new XmlDocument();

        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                    "<title>Pride And Prejudice</title>" +
                    "</book>");

        //Create an attribute collection and remove an attribute
        //from the collection.
        XmlAttributeCollection attrColl = doc.DocumentElement.Attributes;

        attrColl.RemoveAt(0);

        Console.WriteLine("Display the modified XML...\r\n");
        Console.WriteLine(doc.OuterXml);
    }
Пример #4
0
        public void RemoveAtDeletesAttr()
        {
            XmlDocument  doc = CreateDocumentWithElement();
            XmlElement   element = doc.DocumentElement;
            XmlAttribute attr1, attr2;

            attr1 = element.Attributes.Append(doc.CreateAttribute("attr1"));
            element.Attributes.Append(doc.CreateAttribute("attr2"));
            attr2 = element.Attributes.Append(doc.CreateAttribute("attr3"));

            XmlAttributeCollection target = element.Attributes;

            target.RemoveAt(1);

            Assert.Equal(2, target.Count);
            Assert.Same(attr1, target[0]);
            Assert.Same(attr2, target[1]);
        }
Пример #5
0
        public void Remove()
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml("<root a1='garnet' a2='amethyst' a3='bloodstone' a4='diamond' a5='emerald' a6='pearl' a7='ruby' a8='sapphire' a9='moonstone' a10='opal' a11='topaz' a12='turquoize' />");
            XmlElement             el  = xmlDoc.DocumentElement;
            XmlAttributeCollection col = el.Attributes;

            // Remove
            XmlAttribute attr = col.Remove(el.GetAttributeNode("a12"));

            Assert.AreEqual(11, col.Count, "Remove");
            Assert.AreEqual("a12", attr.Name, "Remove.Removed");

            // RemoveAt
            attr = col.RemoveAt(5);
            Assert.AreEqual(null, el.GetAttributeNode("a6"), "RemoveAt");
            Assert.AreEqual("pearl", attr.Value, "Remove.Removed");
        }
Пример #6
0
        static void SortXmlAttributeCollection(XmlAttributeCollection col)
        {
            // Remove, sort, then re-add the attributes to the collection.
            if (sort_attr && col != null && col.Count > 0)
            {
                List <XmlAttribute> attrs = new List <XmlAttribute>(col.Count);

                for (int i = col.Count - 1; i >= 0; i--)
                {
                    attrs.Add(col[i]);
                    col.RemoveAt(i);
                }

                SortAttributeList(attrs);

                for (int i = 0; i < attrs.Count; i++)
                {
                    col.Append(attrs[i]);
                }
            }
        }
Пример #7
0
        private static void SortXmlAttributeCollection(XmlAttributeCollection col)
        {
            // Remove, sort, then re-add the attributes to the collection.
            if (!sort_attr || col == null || col.Count <= 0)
            {
                return;
            }
            var attrs = new List <XmlAttribute>(col.Count);

            for (var i = col.Count - 1; i >= 0; i--)
            {
                attrs.Add(col[i]);
                col.RemoveAt(i);
            }

            SortAttributeList(attrs);
            foreach (var t in attrs)
            {
                col.Append(t);
            }
        }
Пример #8
0
        // Methods
        internal override void Apply(XmlNode parent, ref XmlNode currentPosition)
        {
            Debug.Assert(_matchNode.ParentNode == parent ||
                         (_matchNode.ParentNode == null && _matchNode.NodeType == XmlNodeType.Attribute) ||
                         _matchNode.NodeType == XmlNodeType.XmlDeclaration ||
                         _matchNode.NodeType == XmlNodeType.DocumentType);

            switch (_matchNode.NodeType)
            {
            case XmlNodeType.Element:
            {
                Debug.Assert(_value == null);

                if (_name == null)
                {
                    _name = ((XmlElement)_matchNode).LocalName;
                }
                if (_ns == null)
                {
                    _ns = ((XmlElement)_matchNode).NamespaceURI;
                }
                if (_prefix == null)
                {
                    _prefix = ((XmlElement)_matchNode).Prefix;
                }

                XmlElement newEl = parent.OwnerDocument.CreateElement(_prefix, _name, _ns);

                // move attributes
                XmlAttributeCollection attrs = _matchNode.Attributes;
                while (attrs.Count > 0)
                {
                    XmlAttribute attr = (XmlAttribute)attrs.Item(0);
                    attrs.RemoveAt(0);
                    newEl.Attributes.Append(attr);
                }

                // move children
                XmlNode curChild = _matchNode.FirstChild;
                while (curChild != null)
                {
                    XmlNode nextSibling = curChild.NextSibling;
                    _matchNode.RemoveChild(curChild);
                    newEl.AppendChild(curChild);
                    curChild = nextSibling;
                }

                parent.ReplaceChild(newEl, _matchNode);
                currentPosition = newEl;

                ApplyChildren(newEl);

                break;
            }

            case XmlNodeType.Attribute:
            {
                if (_name == null)
                {
                    _name = ((XmlAttribute)_matchNode).LocalName;
                }
                if (_ns == null)
                {
                    _ns = ((XmlAttribute)_matchNode).NamespaceURI;
                }
                if (_prefix == null)
                {
                    _prefix = ((XmlAttribute)_matchNode).Prefix;
                }
                if (_value == null)
                {
                    _value = ((XmlAttribute)_matchNode).Value;
                }

                XmlAttribute newAttr = parent.OwnerDocument.CreateAttribute(_prefix, _name, _ns);
                newAttr.Value = _value;

                parent.Attributes.Remove((XmlAttribute)_matchNode);
                parent.Attributes.Append(newAttr);
                break;
            }

            case XmlNodeType.Text:
            case XmlNodeType.CDATA:
            case XmlNodeType.Comment:
                Debug.Assert(_value != null);
                ((XmlCharacterData)_matchNode).Data = _value;
                currentPosition = _matchNode;
                break;

            case XmlNodeType.ProcessingInstruction:
            {
                if (_name != null)
                {
                    if (_value == null)
                    {
                        _value = ((XmlProcessingInstruction)_matchNode).Data;
                    }
                    XmlProcessingInstruction newPi = parent.OwnerDocument.CreateProcessingInstruction(_name, _value);

                    parent.ReplaceChild(newPi, _matchNode);
                    currentPosition = newPi;
                }
                else
                {
                    ((XmlProcessingInstruction)_matchNode).Data = _value;
                    currentPosition = _matchNode;
                }
                break;
            }

            case XmlNodeType.EntityReference:
            {
#if NETCORE
                throw new NotSupportedException("XmlNodeType.EntityReference is not supported");
#else
                Debug.Assert(_name != null);

                XmlEntityReference newEr = parent.OwnerDocument.CreateEntityReference(_name);

                parent.ReplaceChild(newEr, _matchNode);
                currentPosition = newEr;
                break;
#endif
            }

            case XmlNodeType.XmlDeclaration:
            {
                Debug.Assert(_value != null && _value != string.Empty);
                XmlDeclaration xmlDecl = (XmlDeclaration)_matchNode;
                xmlDecl.Encoding   = null;
                xmlDecl.Standalone = null;
                xmlDecl.InnerText  = _value;
                break;
            }

            case XmlNodeType.DocumentType:
            {
#if NETCORE
                throw new NotSupportedException("XmlNodeType.DocumentType is not supported");
#else
                if (_name == null)
                {
                    _name = ((XmlDocumentType)_matchNode).LocalName;
                }

                if (_ns == null)
                {
                    _ns = ((XmlDocumentType)_matchNode).SystemId;
                }
                else if (_ns == string.Empty)
                {
                    _ns = null;
                }

                if (_prefix == null)
                {
                    _prefix = ((XmlDocumentType)_matchNode).PublicId;
                }
                else if (_prefix == string.Empty)
                {
                    _prefix = null;
                }

                if (_value == null)
                {
                    _value = ((XmlDocumentType)_matchNode).InternalSubset;
                }

                XmlDocumentType docType = _matchNode.OwnerDocument.CreateDocumentType(_name, _prefix, _ns, _value);
                _matchNode.ParentNode.ReplaceChild(docType, _matchNode);
                break;
#endif
            }

            default:
                Debug.Assert(false);
                break;
            }
        }