OOP篇 10xml

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结构:


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# XML文件读写操作源码,以及如何调用,注释详解,有任何问题请留言, 以下截取xml文件和部分调用代码段: * ++++++++++++++++++++++++++++++++++++++ <?xml version="1.0" encoding="utf-8" standalone="no"?> <!--TestPlugins的信息--> <!--DataPlugins的信息--> * ++++++++++++++++++++++++++++ xml xl = new xml(); xl.XMLWriteRootNode("info"); //XmlElement Eml1 = xl.XMLReadNode("",0); //XmlElement Eml2 = xl.XMLReadNode("DataPlugins", 1); //XmlElement Eml4 = xl.XMLReadNode("DeviceInfo", 2); // TestPlugins XmlElement testPlugins = xl.XMLCreateNode("TestPlugins", null, null); xl.XMLInsertNode("info", 0, "TestPlugins的信息", testPlugins); // FixturePlugin XmlElement fixturePlugin = xl.XMLCreateNode("TestPlugin", null, new Dictionary() { { "Type", "FixturePlugin" } }); xl.XMLInsertNode(testPlugins.LocalName, 1, null, fixturePlugin); // DUTPlugin XmlElement DUTPlugin = (XmlElement)fixturePlugin.CloneNode(true);// xl.XMLCreateNode("TestPlugin", null, new Dictionary() { { "Type", "DUTPlugin" } }); DUTPlugin.SetAttribute("Type", "DUTPlugin"); xl.XMLInsertNode(testPlugins.LocalName, 1, null, DUTPlugin); // Agilent34461APlugin XmlElement Agilent34461APlugin = xl.XMLCreateNode("TestPlugin", null, new Dictionary() { { "Type", "Agilent34461APlugin" } }); xl.XMLInsertNode(testPlugins.LocalName, 1, null, Agilent34461APlugin); // ================================== // DataPlugins XmlElement dataPlugins = xl.XMLCreateNode("DataPlugins", null, null); xl.XMLInsertNode("info", 0, "DataPlugins的信息", dataPlugins); // CSVLogPlugin XmlElement csvlogPlugin = xl.XMLCreateNode("DataPlugin", null, new Dictionary() { { "Type", "CSVLogPlugin" } }); xl.XMLInsertNode(dataPlugins.LocalName, 1, null, csvlogPlugin); XmlElement uartlogPlugin = (XmlElement)csvlogPlugin.CloneNode(true); uartlogPlugin.SetAttribute("Type", "UartLogPlugin"); xl.XMLInsertNode(dataPlugins.LocalName, 1, null, uartlogPlugin); XmlElement testlogPlugin = (XmlElement)csvlogPlugin.CloneNode(true); testlogPlugin.SetAttribute("Type", "TestLogPlugin"); xl.XMLInsertNode(dataPlugins.LocalName, 1, null, testlogPlugin); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值