c#创建xml文件并保存到指定位置

#region 把panel布局控件写到xml文件放到指定地址
        public void writeXml() {
             XmlDocument doc = new XmlDocument();//新建一个xml文档
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));//xml文件头
            XmlElement Root = doc.CreateElement("Root");//根元素
            doc.AppendChild(Root);

            for(int m = 0; m                 //String child = m.ToString();
                //添加元素到xml文件里面 把控件的坐标和大小放到xml文件里面去
                XmlElement child = doc.CreateElement("Element");
                XmlAttribute attr0 = doc.CreateAttribute("Id");
                attr0.Value = m.ToString();
                XmlAttribute attr1 = doc.CreateAttribute("name");
                attr1.Value = panel1.Controls[m].Text;
                 //添加属性到xml文件里面
                child.Attributes.Append(attr0);
                child.Attributes.Append(attr1);
                Root.AppendChild(child);
            }
            doc.Save("d://DemandInfo.xml");//保存用户设计的屏幕布局信息到指定位置
            MessageBox.Show("保存成功!");//提示用户保存成功
        }
        #endregion

你可能感兴趣的:(C#)