C# 写xml文件

            XmlDocument MyXmlDocument = new XmlDocument();
            //创建类型声明节点  
            XmlDeclaration xdDec = MyXmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
            MyXmlDocument.AppendChild(xdDec);
            //创建根节点  
            XmlElement xeRoot = MyXmlDocument.CreateElement("xmlconfig");
            //给节点属性赋值
            xeRoot.SetAttribute("version", "1.0");
            xeRoot.SetAttribute("name", "xml");
            MyXmlDocument.AppendChild(xeRoot);
            //xeRoot = MyXmlDocument.CreateElement("xmlconfig");
            XmlNode xnXwsp = MyXmlDocument.SelectSingleNode("xmlconfig");
            //if (xnXwsp != null)
            //{
            //    xnXwsp.AppendChild(xeRoot);
            //}
            //控制一级节点循环
            for (int i = 0; i < 5; i++)
            {
                XmlNode CurrentSvgInfoName;
                XmlNode svgInfoNameId;
                XmlNode Text;
                XmlNode DataSortint;
                XmlNode DataPara;
                XmlNode DataCompany;
                XmlNode NewNode = MyXmlDocument.CreateElement("config");
                CurrentSvgInfoName = MyXmlDocument.CreateAttribute("CurrentSvgInfoName");
                svgInfoNameId = MyXmlDocument.CreateAttribute("svgInfoNameId");
                Text = MyXmlDocument.CreateAttribute("Text");
                DataSortint = MyXmlDocument.CreateAttribute("DataSortint");
                DataPara = MyXmlDocument.CreateAttribute("DataPara");
                DataCompany = MyXmlDocument.CreateAttribute("DataCompany");
                svgInfoNameId.Value = Convert.ToString(i);
                NewNode.Attributes.SetNamedItem(CurrentSvgInfoName);
                NewNode.Attributes.SetNamedItem(svgInfoNameId);
                xnXwsp.AppendChild(NewNode);
                //控制二级节点循环
                for (int j = 0; j < 3; j++)
                {
                    XmlNode NewNode1 = MyXmlDocument.CreateElement("config");
                    NewNode1.Attributes.SetNamedItem(Text);
                    NewNode1.Attributes.SetNamedItem(DataSortint);
                    NewNode1.Attributes.SetNamedItem(DataPara);
                    NewNode1.Attributes.SetNamedItem(DataCompany);
                    NewNode.AppendChild(NewNode1);
                }
            }
           //保存的路径和文件名
           // MyXmlDocument.Save(path + fileName + ".xml");

Guess you like

Origin blog.csdn.net/qq_14874791/article/details/116704923