Ejemplo n.º 1
0
        public XmlTreeNode GetTreeNode(XmlDocument owner, XmlTreeNode target, XmlTreeView view)
        {
            XmlTreeNode node = view.CreateTreeNode();

            if (this.img == -1 && this.xml != null){
                Regex regex = new Regex(@"[:_.\w]+\s*=\s*(""[^""]*"")|('[^']*')\s*");
                Match m = regex.Match(xml);
                string trimmed = xml.Trim();
                if (m.Success && m.Index == 0 && m.Length == xml.Length) {
                    nodeType = (int)XmlNodeType.Attribute;
                    img = (int)NodeImage.Attribute - 1;
                } else if (trimmed.StartsWith("<?")) {
                    nodeType = (int)XmlNodeType.ProcessingInstruction;
                    img = (int)NodeImage.PI - 1;
                } else if (trimmed.StartsWith("<!--")) {
                    nodeType = (int)XmlNodeType.Comment;
                    img = (int)NodeImage.Comment - 1;
                } else if (trimmed.StartsWith("<![CDATA[")) {
                    nodeType = (int)XmlNodeType.CDATA;
                    img = (int)NodeImage.CData - 1;
                } else if (trimmed.StartsWith("<")) {
                    nodeType = (int)XmlNodeType.Element;
                    img = (int)NodeImage.Element - 1;
                } else {
                    nodeType = (int)XmlNodeType.Text;
                    img = (int)NodeImage.Text - 1;
                }
            }

            XmlNode xn = null;
            XmlNode context = (target != null) ? target.Node : owner;

            if (this.nodeType == (int)XmlNodeType.Attribute) {
                int i = this.xml.IndexOf('=');
                if (i > 0) {
                    string name = this.xml.Substring(0, i).Trim();
                    XmlName qname = XmlHelpers.ParseName(context, name, XmlNodeType.Attribute);
                    xn = owner.CreateAttribute(qname.Prefix, qname.LocalName, qname.NamespaceUri);
                    string s = this.xml.Substring(i + 1).Trim();
                    if (s.Length > 2) {
                        char quote = s[0];
                        s = s.Substring(1, s.Length - 2); // strip off quotes
                        // un-escape quotes in the value.
                        xn.Value = s.Replace(quote == '\'' ? "&apos;" : "&quot;", quote.ToString());
                    }
                }

            } else {
                XmlNamespaceManager nsmgr = XmlHelpers.GetNamespaceScope(context);
                XmlParserContext pcontext = new XmlParserContext(owner.NameTable, nsmgr, null, XmlSpace.None);
                XmlTextReader r = null;
                if (this.xml != null) {
                    r = new XmlTextReader(this.xml, XmlNodeType.Element, pcontext);
                } else {
                    r = new XmlTextReader(this.stm, XmlNodeType.Element, pcontext);
                }
                r.WhitespaceHandling = WhitespaceHandling.Significant;

                // TODO: add multi-select support, so we can insert multiple nodes also.
                // And find valid nodes (for example, don't attempt to insert Xml declaration
                // if target node is not at the top of the document, etc).
                // For now we just favor XML elements over other node types.
                ArrayList list = new ArrayList();
                while (true){
                    XmlNode rn = owner.ReadNode(r);
                    if (rn == null)
                        break;
                    if (rn is XmlElement){
                        xn = rn;
                        NormalizeNamespaces((XmlElement)rn, nsmgr);
                    }
                    list.Add(rn);
                }
                if (xn == null && list.Count>0)
                    xn = list[0] as XmlNode;
            }
            node.Node = xn;

            if (!(xn is XmlAttribute)) {
                view.Invalidate();
                if (xn is XmlElement){
                    if (node.Nodes.Count <= 1){
                        this.img = ((int)NodeImage.Leaf -1);
                    }
                }
            }
            return node;
        }