C# Xml文件的读写总结

1  Xml写入

C# Xml文件的读写总结_第1张图片

代码:

    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:
        /// 
        /// 设备信息更新
        /// 
        /// 
        /// 
        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文件结构:

C# Xml文件的读写总结_第2张图片

6 数据更新 - 2

C# Code:
        /// 
        /// 热备份保存
        /// 
        /// 设备id
        /// 节点路径
        /// 要修改的属性
        /// 要修改的属性内容
        /// 
        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文件的读写总结_第3张图片







































你可能感兴趣的:(C#,开发总结)