关于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);
            }

原文地址:https://www.cnblogs.com/tangtang615/p/1407233.html