C# 写xml文件

15 篇文章 0 订阅

            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");

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值