using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.IO; using System.Data; namespace Ant.Frame { #region 实例应用 //========================================================= //实例应用: //string strXmlFile = Server.MapPath("TestXml.xml"); //XmlControl xmlTool = new XmlControl(strXmlFile); // 数据显视 // dgList.DataSource = xmlTool.GetData("Book/Authors[ISBN=\"0002\"]"); // dgList.DataBind(); // 更新元素内容 // xmlTool.Replace("Book/Authors[ISBN=\"0002\"]/Content","ppppppp"); // xmlTool.Save(); // 添加一个新节点 // xmlTool.InsertNode("Book","Author","ISBN","0004"); // xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Content","aaaaaaaaa"); // xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Title","Sex","man","iiiiiiii"); // xmlTool.Save(); // 删除一个指定节点的所有内容和属性 // xmlTool.Delete("Book/Author[ISBN=\"0004\"]"); // xmlTool.Save(); // 删除一个指定节点的子节点 // xmlTool.Delete("Book/Authors[ISBN=\"0003\"]"); // xmlTool.Save(); #endregion /// /// XmlHelper 的摘要说明。 /// xml操作类 /// public class XmlHelper { protected string strXmlFile; protected XmlDocument objXmlDoc = new XmlDocument(); public XmlHelper(string XmlFile) { // // TODO: 在这里加入建构函式的程序代码 // try { objXmlDoc.Load(XmlFile); } catch (System.Exception ex) { throw ex; } strXmlFile = XmlFile; } public DataTable GetData(string XmlPathNode) { //查找数据。返回一个DataView DataSet ds = new DataSet(); StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml); ds.ReadXml(read); return ds.Tables[0]; } /// /// 新节点内容。 /// 示例:xmlTool.Replace("Book/Authors[ISBN=\"0002\"]/Content","ppppppp"); /// /// /// public void Replace(string XmlPathNode, string Content) { //更新节点内容。 objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content; } /// /// 删除一个指定节点的子节点。 /// 示例: xmlTool.DeleteChild("Book/Authors[ISBN=\"0003\"]"); /// /// public void DeleteChild(string Node) { //删除一个节点。 string mainNode = Node.Substring(0, Node.LastIndexOf("/")); objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node)); } /// /// * 使用示列: /// 示例: XmlHelper.Delete( "/Node", "") /// XmlHelper.Delete( "/Node", "Attribute") /// /// 节点 /// 属性名,非空时删除该节点属性值,否则删除节点值 public void Delete(string node, string attribute) { try { XmlNode xn = objXmlDoc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xn.ParentNode.RemoveChild(xn); else xe.RemoveAttribute(attribute); } catch { } } /// /// 插入一节点和此节点的一子节点。 /// 示例:xmlTool.InsertNode("Book","Author","ISBN","0004"); /// /// 主节点 /// 子节点 /// 元素 /// 内容 public void InsertNode(string MainNode, string ChildNode, string Element, string Content) { //插入一节点和此节点的一子节点。 XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode); XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode); objRootNode.AppendChild(objChildNode); XmlElement objElement = objXmlDoc.CreateElement(Element); objElement.InnerText = Content; objChildNode.AppendChild(objElement); } /// /// 插入一个节点,带一属性。 /// 示例: xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Title","Sex","man","iiiiiiii"); /// /// 主节点 /// 元素 /// 属性 /// 属性内容 /// 元素内容 public void InsertElement(string MainNode, string Element, string Attrib, string AttribContent, string Content) { //插入一个节点,带一属性。 XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode); XmlElement objElement = objXmlDoc.CreateElement(Element); objElement.SetAttribute(Attrib, AttribContent); objElement.InnerText = Content; objNode.AppendChild(objElement); } /// /// 插入一个节点,不带属性。 /// 示例:xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Content","aaaaaaaaa"); /// /// 主节点 /// 元素 /// 元素内容 public void InsertElement(string MainNode, string Element, string Content) { //插入一个节点,不带属性。 XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode); XmlElement objElement = objXmlDoc.CreateElement(Element); objElement.InnerText = Content; objNode.AppendChild(objElement); } /// /// 对xml文件做插入,更新,删除后需做Save()操作,以保存修改 /// public void Save() { //保存文檔。 try { objXmlDoc.Save(strXmlFile); } catch (System.Exception ex) { throw ex; } objXmlDoc = null; } } }