public void CreateRootWithDlg() { //1.打开对话框,询问元素名 ElementNameDlg dlg = new ElementNameDlg (); dlg.SetInfo ("新建根元素", "新建根元素"); dlg.ShowDialog(); if (dlg.DialogResult != DialogResult.OK ) return; if (this.VirtualRoot != null) { Debug.Assert(false,"当前已存在虚根,不可能的情况"); } string strXml = ""; string strName = dlg.textBox_strElementName.Text; string strValue = dlg.textBox_text.Text; strXml = "<" + strName + ">" + strValue + "</" + strName + ">"; this.SetXml(strXml); }
// return: // -1 error // 0 successed // -2 取消 public int InsertChildWithDlg(Item startItem, out string strError) { strError = ""; ElementItem myParent = this.m_selectedItem.parent ; if (myParent == null) { strError = "父亲为null,不可能的情况"; return -1; } //1.打开"新同级"对话框,得到元素名 ElementNameDlg dlg = new ElementNameDlg (); dlg.SetInfo ("新同级元素", "给'" + this.m_selectedItem.Name + "'元素增加新同级元素"); dlg.ShowDialog (); if (dlg.DialogResult != DialogResult.OK ) return -2; // 3.创建一个元素 ElementItem siblingItem = null; int nRet = this.CreateElementItemFromUI(dlg.textBox_strElementName.Text, dlg.textBox_URI.Text, out siblingItem, out strError); if (nRet == -1) return -1; TextItem textItem = null; if (dlg.textBox_text.Text != "") { textItem = this.CreateTextItem(); textItem.SetValue(dlg.textBox_text.Text); siblingItem.AppendChildInternal(textItem,false,false); //一个儿子带着下级,内存对象加大多少 } // 4.加到当前元素的前方 // TODO: try // Exception: // 可能会抛出PrefixNotDefineException异常 nRet = myParent.InsertChild(this.m_selectedItem, siblingItem, out strError); if (nRet == -1) return -1; return 0; }
// 同级前插元素 private void menuItem_InsertSiblingChild(object sender, System.EventArgs e) { Item item = this.m_selectedItem; if (item == null) { MessageBox.Show(this,"尚未选择基准节点"); return; } // 虚根不能插入同级 if (item == this.VirtualRoot) { MessageBox.Show (this,"虚根节点不能插入同级元素"); return; } //文档根节点不能插入同级 if (item == this.docRoot) { MessageBox.Show (this,"文档根节点不能插入同级元素"); return; } ElementItem myParent = item.parent ; if (myParent == null) { MessageBox.Show(this,"父亲为null,不可能的情况"); return; } bool bInputUri = false; string strOldElementName = null; REDOINPUT: //1.打开"新同级"对话框,得到元素名 ElementNameDlg dlg = new ElementNameDlg(); dlg.InputUri = bInputUri; if (String.IsNullOrEmpty(strOldElementName) == false) dlg.textBox_strElementName.Text = strOldElementName; dlg.SetInfo ("新同级元素", "给'" + item.Name + "'元素增加新同级元素"); dlg.ShowDialog(); if (dlg.DialogResult != DialogResult.OK ) return; string strError; // 3.创建一个元素 ElementItem siblingItem = null; int nRet = this.CreateElementItemFromUI( dlg.textBox_strElementName.Text, dlg.textBox_URI.Text, out siblingItem, out strError); if (nRet == -1) { MessageBox.Show(this,strError); return; } // 4.加到当前元素的前方 try { nRet = myParent.InsertChild(item, siblingItem, out strError); } catch (PrefixNotDefineException ex) // 前缀字符串没有找到URI { MessageBox.Show(this, ex.Message); strOldElementName = dlg.textBox_strElementName.Text; bInputUri = true; // 特别要求输入URI字符串 goto REDOINPUT; // 要求重新输入 } if (nRet == -1) { MessageBox.Show(this,strError); return; } TextItem textItem = null; if (dlg.textBox_text.Text != "") { textItem = this.CreateTextItem(); textItem.Value = dlg.textBox_text.Text; siblingItem.AppendChild(textItem); } return; }
private void menuItem_AppendChild(object sender, System.EventArgs e) { Item item = this.m_selectedItem; if (item == null) { MessageBox.Show(this,"尚未选择基准节点"); return; } if (!(item is ElementItem)) { MessageBox.Show(this,"当前节点类型不合法,必须是ElementItem类型"); return; } ElementItem element = (ElementItem)item; //1.弹出"新下级元素"对话框,得到元素名 ElementNameDlg dlg = new ElementNameDlg (); dlg.SetInfo ("新下级元素", "给'" + element.Name + "'追加新下级元素"); dlg.ShowDialog (); if (dlg.DialogResult != DialogResult.OK) return; // 3.创建节点 string strError; ElementItem childItem = null; int nRet = this.CreateElementItemFromUI( dlg.textBox_strElementName.Text, dlg.textBox_URI.Text, out childItem, out strError); if (nRet == -1) { MessageBox.Show(this,strError); return; } // 4.加入到父亲 element.AutoAppendChild(childItem); TextItem textItem = null; if (dlg.textBox_text.Text != "") { textItem = this.CreateTextItem(); textItem.Value = dlg.textBox_text.Text; childItem.AutoAppendChild(textItem); // 一个儿子带着下级,内存对象加大多少 } return; }