xml结构
<?xml version="1.0" encoding="UTF-8"?> <dataTable> <node id="201603220952208053861"> <title>关于软件</title> <type>test</type> <addTime>2016-03-22 09:52:20</addTime> <editTime>2016-03-22 09:52:20</editTime> <content>产品:node 版本:1.0 作者:HUJQ</content> </node> <node id="201603220952563455956"> <title>test</title> <type>测试</type> <addTime>2016-03-22 09:52:56</addTime> <editTime>2016-03-22 09:52:56</editTime> <content>测试</content> </node> </dataTable>
xml类
public class XmlHelper { private XmlDocument objXmlDoc = new XmlDocument(); private string XmlFile; public XmlHelper(string XmlFile) { this.XmlFile = XmlFile; if (!File.Exists(XmlFile)) { creatXml(); } objXmlDoc.Load(XmlFile); } #region 创建xml文件 private void creatXml() { //加入XML的声明段落,<?xml version="1.0" encoding="UTF-8"?> XmlDeclaration xmldecl = objXmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); //添加上述创建和 xml声明节点 objXmlDoc.AppendChild(xmldecl); //创建xml dbGuest 元素(根节点) XmlElement xmlelem = objXmlDoc.CreateElement("", "dataTable", ""); objXmlDoc.AppendChild(xmlelem); try { objXmlDoc.Save(XmlFile); } catch (Exception ex) { } Node node = new Node() { id = getID(), title = "关于软件", type = "test", addTime = getDateString(), editTime = getDateString(), content = "产品:node 版本:1.0 作者:HUJQ" }; addXml(node); } #endregion #region 读xml文件 public List<Node> readXml() { List<Node> nodeList = new List<Node>(); XmlNode root = objXmlDoc.SelectSingleNode("dataTable");// 得到根节点 XmlNodeList xmlNodeList = root.ChildNodes;// 得到根节点的所有子节点 foreach (XmlNode xnl in xmlNodeList) { Node node = new Node(); // 将节点转换为元素,便于得到节点的属性值 XmlElement xe = (XmlElement)xnl; // 得到id属性的属性值 node.id = xe.GetAttribute("id"); node.title = xe.GetElementsByTagName("title")[0].InnerText; node.type = xe.GetElementsByTagName("type")[0].InnerText; node.addTime = xe.GetElementsByTagName("addTime")[0].InnerText; node.editTime = xe.GetElementsByTagName("editTime")[0].InnerText; node.content = xe.GetElementsByTagName("content")[0].InnerText; nodeList.Add(node); } return nodeList; } #endregion #region 添加xml public void addXml(Node node) { XmlNode root = objXmlDoc.SelectSingleNode("dataTable"); XmlElement xe1 = objXmlDoc.CreateElement("node");//创建一个<Node>节点 xe1.SetAttribute("id", node.id);//设置该节点genre属性 XmlElement xesub1 = objXmlDoc.CreateElement("title"); xesub1.InnerText = node.title;//设置文本节点 xe1.AppendChild(xesub1);//添加到<Node>节点中 XmlElement xesub2 = objXmlDoc.CreateElement("type"); xesub2.InnerText = node.type; xe1.AppendChild(xesub2); XmlElement xesub3 = objXmlDoc.CreateElement("addTime"); xesub3.InnerText = node.addTime; xe1.AppendChild(xesub3); XmlElement xesub4 = objXmlDoc.CreateElement("editTime"); xesub4.InnerText = node.editTime; xe1.AppendChild(xesub4); XmlElement xesub5 = objXmlDoc.CreateElement("content"); xesub5.InnerText = node.content; xe1.AppendChild(xesub5); root.AppendChild(xe1);//添加到<Employees>节点中 //保存创建好的XML文档 objXmlDoc.Save(XmlFile); } #endregion #region 删除xml public void delXml(string id) { XmlNodeList xmlNodeList = objXmlDoc.SelectSingleNode("dataTable").ChildNodes; foreach (XmlNode xn in xmlNodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn;//将子节点类型转换为aXmlElement类型 if (xe.GetAttribute("id") == id) { xe.ParentNode.RemoveChild(xe); } } objXmlDoc.Save(XmlFile); } #endregion #region 修改xml public void edidXml(Node node) { XmlNodeList xmlNodeList = objXmlDoc.SelectSingleNode("dataTable").ChildNodes;//获取dbGuest节点的所有子节点 foreach (XmlNode xn in xmlNodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn;//将子节点类型转换为aXmlElement类型 if (xe.GetAttribute("id") == node.id) { xe.GetElementsByTagName("title")[0].InnerText = node.title; xe.GetElementsByTagName("type")[0].InnerText = node.type; xe.GetElementsByTagName("editTime")[0].InnerText = node.editTime; xe.GetElementsByTagName("content")[0].InnerText = node.content; } } objXmlDoc.Save(XmlFile); } #endregion public string getID() { return DateTime.Now.ToString("yyyyMMddHHmmssfffffff"); } public string getDateString() { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } }