【笔记】C#对xml文件的读写操作

it2022-05-05  190

using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using System.Xml; using System; public class _ControlLog : MonoBehaviour { /// <summary> /// 对xml文件进行操作的方法事务 /// </summary> // Use this for initialization void Start() { string xmlpath = Application.dataPath + @"/StreamingAssets/check_log.xml"; if (File.Exists(xmlpath)) { } } //创建XML public static void CreatXmlFun(string path, string mode) { XmlTextWriter writer = new XmlTextWriter(path, System.Text.Encoding.UTF8); writer.WriteStartDocument(); writer.WriteStartElement(mode + "_log"); //关闭根元素,并书写结束标签 writer.WriteEndElement(); //将XML写入文件 writer.Close(); } //添加数据 public static void AddXmlInformation(string xmlFilePath, _CheckLogObject opera) { DateTime S = DateTime.Now.ToLocalTime(); string nowTime = S.ToString("yyyy-MM-dd HH:mm:ss"); opera.Time = nowTime; string operCont_str = opera.OperCont; string time_str = opera.Time; string decide_str = opera.Decide.ToString(); try { XmlDocument myXmlDoc = new XmlDocument(); myXmlDoc.Load(xmlFilePath); //获取根节点 XmlNode root = myXmlDoc.DocumentElement; int i=root.ChildNodes.Count; //新建一个子节点,并赋予id XmlElement newElement = myXmlDoc.CreateElement("check_set"); newElement.SetAttribute("id", i.ToString()); root.AppendChild(newElement); //添加孙节点,填入数据 XmlElement operCont = myXmlDoc.CreateElement("operCont"); operCont.InnerText = operCont_str; newElement.AppendChild(operCont); XmlElement time = myXmlDoc.CreateElement("time"); time.InnerText = time_str; newElement.AppendChild(time); XmlElement decide = myXmlDoc.CreateElement("decide"); decide.InnerText = decide_str; newElement.AppendChild(decide); //保存更改 myXmlDoc.Save(xmlFilePath); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } //读取数据 public static void ReadXml(string xmlFilePath, List<_CheckLogObject> opera) { try { XmlDocument myXmlDoc = new XmlDocument(); myXmlDoc.Load(xmlFilePath); XmlNode root = myXmlDoc.DocumentElement; int i = 0; foreach (XmlNode child in root.ChildNodes) { Debug.Log(child.Attributes["id"].Value); opera.Add(new _CheckLogObject()); foreach (XmlNode Chidren in child.ChildNodes) { switch (Chidren.Name) { //操作内容 case "operCont": opera[i].OperCont = Chidren.InnerText; break; //操作发生时间 case "time": opera[i].Time = Chidren.InnerText; break; //操作描述 case "decide": opera[i].Decide = bool.Parse(Chidren.InnerText); break; } Debug.Log(Chidren.InnerText); } i++; //Debug.Log(w++); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } //删除节点 public static void DelTest(string xmlFilePath) { try { XmlDocument myXmlDoc = new XmlDocument(); myXmlDoc.Load(xmlFilePath); XmlNode root = myXmlDoc.DocumentElement; Debug.Log(root.ChildNodes.Count); //删除root节点下所有节点 root.RemoveAll(); //删除root节点下某一个节点 //root.RemoveChild(XmlNode); myXmlDoc.Save(xmlFilePath); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }

对应的Xml格式:

<?xml version="1.0" encoding="UTF-8"?> <oper_log> <oper id="0"> <operCont>打开程序</operCont> <time>2019-7-17 18:30:20</time> <decide>false</decide> </oper> <oper id="1"> <operCont>上厕所</operCont> <time>2019-7-17 18:32:20</time> <decide>true</decide> </oper> <oper id="2"> <operCont>上第二次厕所</operCont> <time>2019-7-17 18:40:20</time> <decide>false</decide> </oper> <oper id="3"> <operCont>操作内容</operCont> <time>2019-7-17 18:45:20</time> <decide>true</decide> </oper> <oper id="4"> <operCont >还是上班</operCont> <time>2019-7-17 18:55:20</time> <decide>true</decide> </oper> <oper id="5"> <operCont>星期四</operCont> <time>2019-7-17 18:56:20</time> <decide>true</decide> </oper> <oper id="6"> <operCont>操作内容</operCont> <time>2019-7-17 18:58:20</time> <decide>false</decide> </oper> </oper_log>

 


最新回复(0)