| string str = Server.MapPath("Contack.xml"); XmlDocument xDoc = new XmlDocument(); xDoc.Load(str); XmlElement xEle = null; //查看要修改的元素是否存在 xEle = (XmlElement)xDoc.SelectSingleNode(string.Format("//Name[First=''''{0}'''' and Last=''''{1}'''']",FirstName.Text,LastName.Text)); if (xEle==null) //不存在 { Label5.Text = "no exist"; } else //存在 { //下面是一种方法 /*xEle.ChildNodes[0].InnerXml = FirstName.Text + "Replace"; xEle.ChildNodes[1].InnerXml = LastName.Text + "Replace"; xEle.NextSibling.InnerXml = FirstName.Text + "and" + LastName.Text + "Replace";*/ //也可以这样 //true表示递归地克隆指定节点下的子树 XmlElement xNodeCopy = (XmlElement)xEle.CloneNode(true);// xNodeCopy.ChildNodes[0].InnerXml = FirstName.Text + "Replace"; xNodeCopy.ChildNodes[1].InnerXml = LastName.Text + "Replace"; //xNodeCopy.NextSibling.InnerXml = FirstName.Text + "and" + LastName.Text + "Replace"; //xNodeCopy.ParentNode.ReplaceChild(xNodeCopy,xEle); //这样是错误的,因为xNodeCopy只是clone了这个点及子点,并没有父点 xEle.ParentNode.ReplaceChild(xNodeCopy,xEle); } xDoc.Save(str); } 3.3 删除string str; str = Server.MapPath("Contack.xml"); XmlDocument xDoc = new XmlDocument(); xDoc.Load(str); XmlElement xEle = null; //查找是否有存在指定的点 xEle = (XmlElement)xDoc.SelectSingleNode(string.Format("//Name[First=''''{0}'''' and Last=''''{1}'''']",FirstName.Text,LastName.Text)); if (xEle!=null) //存在 { 上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] 下一页 |