在XML中写入数据

前端之家收集整理的这篇文章主要介绍了在XML中写入数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
XmlDocument xmldoc = new XmlDocument(); //声明节 XmlDeclaration dec = xmldoc.CreateXmlDeclaration("1.0","utf-8",null); xmldoc.AppendChild(dec); //加入一个根节点 XmlElement oneNode = xmldoc.CreateElement("CATALOG"); //创建节点 XmlElement twoNode = xmldoc.CreateElement("CD"); XmlElement twoNodeone = xmldoc.CreateElement("TITLE"); twoNodeone.InnerText = "Empire Burlesque"; twoNode.AppendChild(twoNodeone);//添加到CD节点下面 XmlElement twoNodeone1 = xmldoc.CreateElement("ARTIST"); twoNodeone1.InnerText = "Bob Dylan"; twoNode.AppendChild(twoNodeone1);//添加到CD节点下面 XmlElement twoNodeone2 = xmldoc.CreateElement("COUNTRY"); twoNodeone2.InnerText = "USA"; twoNode.AppendChild(twoNodeone2);//添加到CD节点下面 XmlElement twoNodeone3 = xmldoc.CreateElement("COMPANY"); twoNodeone3.InnerText = "Columbia"; twoNode.AppendChild(twoNodeone3);//添加到CD节点下面 XmlElement twoNodeone4 = xmldoc.CreateElement("YEAR"); twoNodeone4.InnerText = "10.90"; twoNode.AppendChild(twoNodeone4);//添加到CD节点下面 XmlElement twoNodeone5 = xmldoc.CreateElement("PRICE"); twoNodeone5.InnerText = "1985"; twoNode.AppendChild(twoNodeone5);//添加到CD节点下面 oneNode.AppendChild(twoNode);//添加到CATALOG节点下面 //创建节点,第二个CD节点 XmlElement twoNode1 = xmldoc.CreateElement("CD"); XmlElement twoNodeone123 = xmldoc.CreateElement("TITLE"); twoNodeone123.InnerText = "Empire Burlesque"; twoNode1.AppendChild(twoNodeone123);//添加到CD节点下面 XmlElement twoNodeone11 = xmldoc.CreateElement("ARTIST"); twoNodeone11.InnerText = "Bob Dylan"; twoNode1.AppendChild(twoNodeone11);//添加到CD节点下面 XmlElement twoNodeone22 = xmldoc.CreateElement("COUNTRY"); twoNodeone22.InnerText = "USA"; twoNode1.AppendChild(twoNodeone22);//添加到CD节点下面 XmlElement twoNodeone33 = xmldoc.CreateElement("COMPANY"); twoNodeone33.InnerText = "Columbia"; twoNode1.AppendChild(twoNodeone33);//添加到CD节点下面 XmlElement twoNodeone44 = xmldoc.CreateElement("YEAR"); twoNodeone44.InnerText = "10.90"; twoNode1.AppendChild(twoNodeone44);//添加到CD节点下面 XmlElement twoNodeone45 = xmldoc.CreateElement("PRICE"); twoNodeone45.InnerText = "1985"; twoNode1.AppendChild(twoNodeone45);//添加到CD节点下面 oneNode.AppendChild(twoNode1);//添加到CATALOG节点下面 xmldoc.AppendChild(oneNode); xmldoc.Save("1.xml");//保存xml 原文链接:https://www.f2er.com/xml/299175.html

猜你在找的XML相关文章