zoukankan      html  css  js  c++  java
  • 关于xml文件

    1,将一些文本框里的值写入xml文件(即保存在xml文件里),如下: 

                        XmlDocument xmlDoc = new XmlDocument();
                        XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                        xmlDoc.AppendChild(xmlDec);
                        //新建根节点iCMS-IpManagement
                        XmlElement xmlElementRoot = xmlDoc.CreateElement("iCMS-IpManagement");
                        xmlDoc.AppendChild(xmlElementRoot);
                        //新建子节点,初始化为iCMS_Node_1
                        XmlElement xmlElementNode = xmlDoc.CreateElement("iCMS_Node");
                        xmlElementNode.SetAttribute("ID", "0");

                        //分别建立服务器的元素ServerName,IpAddress,IpPort
                        XmlElement ElementServerName = xmlDoc.CreateElement("ServerName");
                        ElementServerName.InnerText = this.txtServerName.Text;
                        xmlElementNode.AppendChild(ElementServerName);

                        XmlElement ElementIpAdes = xmlDoc.CreateElement("IpAddress");
                        ElementIpAdes.InnerText = this.txtIpAdes.Text;
                        xmlElementNode.AppendChild(ElementIpAdes);

                        XmlElement ElementPort = xmlDoc.CreateElement("IpPort");
                        ElementPort.InnerText = this.txtPort.Text;
                        xmlElementNode.AppendChild(ElementPort);

                        //填充并保存
                        xmlElementRoot.AppendChild(xmlElementNode);
                        xmlDoc.Save("文件路径");

    2,读取xml文件里的一些信息,如下:

               XmlDocument xmlDoc = new XmlDocument();
               xmlDoc.Load("文件路径");

               [ string xPath = "/iCMS-IpManagement/iCMS_Node[ServerName='" + ServerName + "']";]

                string xPath = "/iCMS-IpManagement/iCMS_Node[@ID='" + strId + "']";
                XmlNodeList nodeList = xmlDoc.SelectNodes(xPath);
                if (nodeList.Count > 0)
                {
                    serverId = Convert.ToInt32(nodeList.Item(0).Attributes["ID"].Value);
                }

  • 相关阅读:
    SpringBoot发送邮箱验证码
    判断一个数是否为2的整数次幂
    [模板] 虚树 && bzoj2286-[Sdoi2011]消耗战
    [模板] K-D Tree
    [模板] 平衡树: Splay, 非旋Treap, 替罪羊树
    对于约数个数上界的估计
    luogu3702-[SDOI2017]序列计数
    [模板] 线性基
    [模板] 区间mex && 区间元素种数
    bzoj4367-[IOI2014]holiday假期
  • 原文地址:https://www.cnblogs.com/tangtang615/p/1407233.html
Copyright © 2011-2022 走看看