| 发表于:2007-09-20 15:02:3519楼 得分:0 |
大致代码如下: xmldocument xmldoc = new xmldocument(); xmldoc.load( "e:\\1.xml "); xmlnodelist elist = xmldoc.documentelement.childnodes; xmlelement newelt = xmldoc.createelement( "item "); string previd = " "; string keyid = " "; string nextid = " "; if (previd.length > 0 && keyid.length > 0 ¦ ¦ previd.length > 0 && nextid.length > 0 ¦ ¦ keyid.length > 0 && nextid.length) { //只有两个以上节点不为 " "的时候才判断,否则直接添加 string xpath = string.format( "/root/item[1> 2 or {0}] ", createxpath(previd, keyid, nextid)); xmlnode node = doc.selectsinglenode(xpath); if (node != null) { //delete node } } //to do append node private string createxpath(string property1, string property2) { stringbuilder sb = new stringbuilder(); sb.append( " or "); sb.append( "(@previd= '{0} ' and @keyid= '{1} ') or (@previd= '{0} ' and @nextid= '{1} ') or (@previd= '{1} ' and @keyid= '{0} ') or (@previd= '{1} ' and @nextid= '{0} ') "); return sb.tostring(); } private string createxpath(string property1, string property2, string property3) { stringbuilder sb = new stringbuilder(); if (property1.length > 0 && property2.length > 0) { sb.append(createxpath(property1, property2)); } if (property1.length > 0 && property3.length > 0) { sb.append(createxpath(property1, property3)); } if (property2.length > 0 && property3.length > 0) { sb.append(createxpath(property2, property3)); } return sb.tostring(); } 你测试测试吧 大致的样子就是这样 上面的逻辑是只有两个以上节点不为 " "的时候才判断,否则直接添加 并且默认原xml文件是合法的 即没有 "重复节点 " 否则把selectsinglenode换成selectnodes | | |
|