在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 public class XmlUtility
========================================================= 2 { 3 protected string strXmlFile; 4 protected XmlDocument objXmlDoc = new XmlDocument(); 5 6 public XmlUtility(string XmlFile) 7 { 8 try 9 { 10 if (!File.Exists(XmlFile)) 11 { 12 CreatXmlFile(XmlFile); 13 } 14 objXmlDoc.Load(XmlFile); 15 16 } 17 catch (System.Exception ex) 18 { 19 throw ex; 20 } 21 strXmlFile = XmlFile; 22 } 23 protected void CreatXmlFile(string file) 24 { 25 XmlTextWriter writer = new 26 XmlTextWriter(file, Encoding.UTF8); 27 28 // start writing! 29 writer.WriteStartDocument(); 30 writer.WriteStartElement("Root"); 31 writer.WriteEndElement(); 32 writer.WriteEndDocument(); 33 writer.Close(); 34 } 35 /// <summary> 36 /// 查找数据。返回一个DataSet 多条 37 /// </summary> 38 /// <param name="XmlPathNode">结点路径</param> 39 /// <returns></returns> 40 public DataSet GetData(string XmlPathNode) 41 { 42 //查找数据。返回一个DataSet 43 DataSet ds = new DataSet(); 44 //=========多个================= 45 foreach(XmlNode xmlnode in objXmlDoc.SelectNodes(XmlPathNode)) 46 { 47 StringReader read = new StringReader(xmlnode.OuterXml); 48 ds.ReadXml(read); 49 } 50 //============================== 51 return ds; 52 } 53 /// <summary> 54 /// 查找数据。返回一个DataSet 单条 55 /// </summary> 56 /// <param name="XmlPathNode"></param> 57 /// <returns></returns> 58 public DataSet GetDataSingle(string XmlPathNode) 59 { 60 //查找数据。返回一个DataSet 61 DataSet ds = new DataSet(); 62 //==========单个================ 63 StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml); 64 ds.ReadXml(read); 65 //============================== 66 return ds; 67 } 68 /// <summary> 69 /// 更新节点内容 70 /// </summary> 71 /// <param name="XmlPathNode"></param> 72 /// <param name="Content"></param> 73 public void Replace(string XmlPathNode, string Content) 74 { 75 //更新节点内容。 76 objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content; 77 } 78 /// <summary> 79 /// 删除一个节点 80 /// </summary> 81 /// <param name="Node"></param> 82 public void Delete(string Node) 83 { 84 //删除一个节点。 85 string mainNode = Node.Substring(0, Node.LastIndexOf("/")); 86 objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node)); 87 } 88 /// <summary> 89 /// 插入一个节点和此节点的一子节点 90 /// </summary> 91 /// <param name="MainNode"></param> 92 /// <param name="ChildNode"></param> 93 /// <param name="Element"></param> 94 /// <param name="Content"></param> 95 public void InsertNode(string MainNode, string ChildNode, string Element, string Content) 96 { 97 //插入一个节点和此节点的一子节点。 98 XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode); 99 XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode); 100 objRootNode.AppendChild(objChildNode); 101 XmlElement objElement = objXmlDoc.CreateElement(Element); 102 objElement.InnerText = Content; 103 objChildNode.AppendChild(objElement); 104 } 105 /// <summary> 106 /// 插入一个节点,带一属性 107 /// </summary> 108 /// <param name="MainNode"></param> 109 /// <param name="Element"></param> 110 /// <param name="Attrib"></param> 111 /// <param name="AttribContent"></param> 112 /// <param name="Content"></param> 113 public void InsertElement(string MainNode, string Element, string Attrib, string AttribContent, string Content) 114 { 115 //插入一个节点,带一属性。 116 XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode); 117 XmlElement objElement = objXmlDoc.CreateElement(Element); 118 objElement.SetAttribute(Attrib, AttribContent); 119 objElement.InnerText = Content; 120 objNode.AppendChild(objElement); 121 } 122 /// <summary> 123 /// 插入一个节点,不带属性 124 /// </summary> 125 /// <param name="MainNode"></param> 126 /// <param name="Element"></param> 127 /// <param name="Content"></param> 128 public void InsertElement(string MainNode, string Element, string Content) 129 { 130 XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode); 131 XmlElement objElement = objXmlDoc.CreateElement(Element); 132 objElement.InnerText = Content; 133 objNode.AppendChild(objElement); 134 } 135 /// <summary> 136 /// 保存文件 137 /// </summary> 138 public void Save() 139 { 140 //保存文件。 141 try 142 { 143 objXmlDoc.Save(strXmlFile); 144 } 145 catch (System.Exception ex) 146 { 147 throw ex; 148 } 149 objXmlDoc = null; 150 } 151 } 实例应用:
string strXmlFile = Server.MapPath("TestXml.xml");
XmlControl xmlTool = new XmlControl(strXmlFile); // 数据显示 条件:节点BID=1所有数据 // dgList.DataSource = xmlTool.GetData("Book/Authors[BID=\"1\"]").Tables[0];; // dgList.DataBind(); // 更新元素內容 条件:节点BID=2 // xmlTool.Replace("Book/Authors[BID=\"2\"]/Content","ppppppp"); // xmlTool.Save(); // 添加一个新节点 条件:节点BID=4 // xmlTool.InsertNode("Book","Author","BID","4"); // xmlTool.InsertElement("Book/Author[BID=\"4\"]","Content","aaaaaaaaa"); // xmlTool.InsertElement("Book/Author[BID=\"4\"]","Title","Sex","man","iiiiiiii"); // xmlTool.Save(); // 删除一个指定节点的所有內容和属性 条件:节点BID=4 // xmlTool.Delete("Book/Author[BID=\"4\"]"); // xmlTool.Save(); // 删除一个指定节点的子节点 条件:节点BID内容为4 // xmlTool.Delete("Book/Authors[BID=\"4\"]"); // xmlTool.Save(); // 删除一个指定节点的子节点 条件:节点AuthorsBID的属性Booktype=9 // xmlTool.Delete("Book/Authors[@Booktype=\"9\"]"); // xmlTool.Save(); |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论