日期:2014-05-17 浏览次数:20385 次
<?xml version="1.0" encoding="utf-8"?> <root> <left> <div1>1</div1> </left> <center> </center> <right> </right> </root>
public static void XmlMoveNode(string xmlPath,string oldNode,string newNode,string node) { ... }
<?xml version="1.0" encoding="utf-8"?> <root> <left> </left> <center> </center> <right> <div1>1</div1> </right> </root>
//XmlMoveNode("..\\..\\test.xml", "//root/left", "//root/right", "//div1"); public static void XmlMoveNode(string xmlPath, string oldNode, string newNode, string node) { XmlDocument xDoc = new XmlDocument(); //加载app.config xDoc.Load(xmlPath); XmlNode xNode = xDoc.SelectSingleNode(oldNode); XmlNode targetNode = xNode.SelectSingleNode(node); ; if (targetNode == null) { throw new Exception("Node not found:" + node); } else { xNode.RemoveChild(targetNode); } XmlNode yNode = xDoc.SelectSingleNode(newNode); yNode.AppendChild(targetNode); xDoc.Save(xmlPath); }