C# Xml文件的读写总结
2016-12-28 18:13
423 查看
1 Xml写入
代码:
string path = "test.xml"; XmlTextWriter writeXml = new XmlTextWriter(path, Encoding.UTF8); writeXml.WriteStartDocument(false); writeXml.WriteStartElement("TemplateList"); writeXml.WriteComment("模板列表"); for (int i = 1; i <= 10; i++) { writeXml.WriteStartElement("Template" + i);//模板名 writeXml.WriteAttributeString("Id", "" + i); writeXml.WriteAttributeString("Name", "模板" + i); writeXml.WriteAttributeString("Unit", "省级"); writeXml.WriteAttributeString("DevType", i % 2 == 0 ? "64" : "128"); writeXml.WriteAttributeString("Version", "V1.0.0"); writeXml.WriteAttributeString("SwitchString", ""); for (int j = 1; j < 3; j++) //测试数据 { writeXml.WriteStartElement("InPort");//输入口 writeXml.WriteAttributeString("InPortId", "" + j); writeXml.WriteAttributeString("TemplateId", "" + i); writeXml.WriteAttributeString("OutPort", "" + i); writeXml.WriteEndElement(); } writeXml.WriteEndElement(); } writeXml.Flush(); writeXml.Close(); }
此案例中,主要用到XmlTextWriter来创建xml,其中
WriteStartElement 创建节点
WriteComment 描述信息
WriteAttributeString 节点的属性
WriteEndElement 节点关闭
Flush 刷新流
Close 关闭写入
值得注意的是,有了WriteStartElement 就必须对应一个WriteEndElement ,创建节点的属性只需要把WriteAttributeString 写在WriteStartElement
的后边即可!
2 获取节点
假如我要获取Template1的所有子节点XmlNodeList nodeList = xmlDoc.SelectSingleNode("TemplateList//Template" + info.TemplateId).ChildNodes;
3 设置属性值
假如我要修改template1的属性值xmlDoc.SelectSingleNode("TemplateList//Template" + info.TemplateId).Attributes["Name"].InnerText = info.Name; xmlDoc.SelectSingleNode("TemplateList//Template" + info.TemplateId).Attributes["Unit"].InnerText = info.Unit; xmlDoc.SelectSingleNode("TemplateList//Template" + info.TemplateId).Attributes["DevType"].InnerText = info.DevType; xmlDoc.SelectSingleNode("TemplateList//Template" + info.TemplateId).Attributes["Version"].InnerText = info.Version; xmlDoc.SelectSingleNode("TemplateList//Template" + info.TemplateId).Attributes["SwitchString"].InnerText = info.SwitchString;
4 遍历节点
假如我要遍历template1的所有子节点,并查找InPortId==“1”的节点,且删掉foreach (XmlNode xn in nodeList) { XmlElement xe = (XmlElement)xn; if (xe.Attributes["InPortId"].InnerText == "1") { // 找到相同项,删除掉 xmlDoc.DocumentElement.SelectSingleNode("Template" + info.TemplateId).RemoveChild(xn); xmlDoc.Save(path); break; } }
5 数据更新 - 1
C# Code:/// <summary> /// 设备信息更新 /// </summary> /// <param name="info"></param> /// <returns></returns> public static bool DeviceUpdate(DeviceDetail info) { bool bo = false; try { XmlDocument xmlDoc = new XmlDocument(); string filePath = GetPath(info.DevId); xmlDoc.Load(filePath); XmlNode node = xmlDoc.SelectSingleNode("MatrixList//Device"); XmlElement ee = (XmlElement)node; ee.SetAttribute("DevName", info.DevName); ee.SetAttribute("DevType", info.DevType); ee.SetAttribute("DevIp", info.DevIp); ee.SetAttribute("DevPort", info.DevPort); xmlDoc.Save(filePath); bo = true; } catch (Exception ex) { Console.Write(ex.Message); } return bo; }
代码调用:
DeviceDetail info = new DeviceDetail(); info.DevId = tbDevId.Text; info.DevName = tbDevName.Text; info.DevType = (cbbDevType.SelectedIndex == 0 ? 64 : 128).ToString(); info.DevIp = tbDevIp.Text; info.DevPort = tbDevPort.Text; bo = MatrixManager.DeviceUpdate(info); if (bo == true) { MessageBox.Show("保存成功,请重启客户端!"); } else { MessageBox.Show("保存失败!"); }
XML文件结构:
6 数据更新 - 2
C# Code:/// <summary> /// 热备份保存 /// </summary> /// <param name="devId">设备id</param> /// <param name="nodeStr">节点路径</param> /// <param name="item">要修改的属性</param> /// <param name="data">要修改的属性内容</param> /// <returns></returns> public static bool HotBackUpdate(string devId, string nodeStr, HotBackInfo info) { bool bo = false; try { XmlDocument xmlDoc = new XmlDocument(); string filePath = GetPath(devId); xmlDoc.Load(filePath); XmlNode node = xmlDoc.SelectSingleNode(nodeStr); XmlElement ee = (XmlElement)node; ee.SetAttribute("ID", info.Card); ee.SetAttribute("Mode", info.Mode); ee.SetAttribute("Chunnel", info.Chunnel); xmlDoc.Save(filePath); bo = true; } catch (Exception ex) { Console.Write(ex.Message); } return bo; }
代码调用:
MatrixManager.HotBackUpdate("1", "MatrixList//HotBack//Card" + (wedgeBox.SelectedIndex + 1), info); MessageBox.Show("更新成功!");
XML结构:
相关文章推荐
- C# 读写XML文件的方法详细总结
- C#操作Excel(创建、打开、读写、保存)几种方法的总结
- c#读写XML文件 (转)
- asp.net读写添加修改删除操作xml文件 代码如下(C#)
- c#读写XML文件 (转)
- c#读写xml文件
- c#读写xml文件
- C# WinForm 读写XML文件
- c#读写xml文件
- C# 读写类操作xml文件
- C#读写XML文件
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- c# 读写xml文件
- C#操作XML文件总结篇
- C#读写xml文件(转载)
- C#读写xml文件实例
- C#读写xml文件
- C#读写控制XML文件(主要为自己设置数据库连接文件参考)
- 【转】c#读写xml文件入门方法
- C# WinForm 读写XML文件