示例#1
0
        public ChangeNode(XmlTreeView view, XmlTreeNode node, XmlNodeType nt)
        {
            this.doc = view.Model.Document;
            this.nt = nt;
            this.view = view;
            this.node = node;
            XmlNode n = node.Node;
            if (n == null) return;

            init:
            this.oldnt = n.NodeType;
            string innerXml = (oldnt == XmlNodeType.Element) ? n.InnerXml : SpecialUnescape(oldnt, n.Value);
            string outerXml = n.OuterXml;
            string qname = n.Name;
            string localName = n.LocalName;
            string ns = n.NamespaceURI;
            bool noName = false;
            if (qname.StartsWith("#")) {
                qname = localName = "";
            }
            noName = string.IsNullOrEmpty(qname);

            if (noName && IsNamedNodeType(nt)) {
                // Try parsing the content of the node as markup! (but first check for special unescaping
                // that we do for nested comment/cdata blocks)
                PasteCommand paste = new PasteCommand(doc, view, InsertPosition.Before, new TreeData(innerXml));
                XmlTreeNode nte = paste.NewNode;
                if (nte != null && IsNamedNodeType(nte.NodeType)) {
                    // then it worked - we extracted a node with a name, so start over.
                    n = newNode = nte.Node;
                    goto init;
                }
            }
            if (newNode == null || newNode.NodeType != nt) {
                switch (nt) {
                    case XmlNodeType.Element:
                        if (noName) {
                            qname = "element";
                        }
                        newNode = doc.CreateElement(qname, ns);
                        newNode.InnerXml = innerXml;
                        break;
                    case XmlNodeType.Attribute:
                        if (noName) {
                            qname = "attribute";
                        }
                        newNode = doc.CreateAttribute(qname, ns);
                        newNode.Value = innerXml;
                        break;
                    case XmlNodeType.Comment:
                        newNode = doc.CreateComment(SpecialEscape(nt, noName ? innerXml : outerXml));
                        break;
                    case XmlNodeType.ProcessingInstruction:
                        if (noName) {
                            localName = "pi";
                        }
                        newNode = doc.CreateProcessingInstruction(localName, innerXml);
                        break;
                    case XmlNodeType.Text:
                        newNode = doc.CreateTextNode(noName ? innerXml : outerXml);
                        break;
                    case XmlNodeType.CDATA:
                        newNode = doc.CreateCDataSection(SpecialEscape(nt, noName ? innerXml : outerXml));
                        break;
                }
            }
            InsertNode icmd = new InsertNode(node, InsertPosition.Before, newNode, true, true);
            newTreeNode = icmd.NewNode;
            DeleteNode del = new DeleteNode(doc, node);
            group = new CompoundCommand(this.Name);
            group.Add(icmd);
            group.Add(del);
        }