c#基础值XML

(1)

      //Step2:创建XML文档对象
            XmlDocument doc = new XmlDocument();

            //Step3:创建第一行描述信息,并添加到doc文档中  Element 与 Node的关系,文档中所有内容都属于Element,其中标签属于Node,
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(dec);

            //Step4:XML必须包含且仅能包含一个根结点
            XmlElement order = doc.CreateElement("Order");
            doc.AppendChild(order);

            //Step5:给根结点创建子结点
            XmlElement customerName = doc.CreateElement("customerName");
            customerName.InnerText = "张三";
            order.AppendChild(customerName);

            //Step6:给customerName添加子结点
            XmlElement customerNumber = doc.CreateElement("customerNumber");
            customerNumber.InnerText = "100000001";
            order.AppendChild(customerNumber);

            XmlElement items = doc.CreateElement("items");
            order.AppendChild(items);

            //Step6:给结点添加属性
            XmlElement orderItem1 = doc.CreateElement("OrderItems");
            orderItem1.SetAttribute("Name", "手机");
            orderItem1.SetAttribute("Count", "1");
            items.AppendChild(orderItem1);

            XmlElement orderItem2 = doc.CreateElement("OrderItems");
            orderItem2.SetAttribute("Name", "手表");
            orderItem2.SetAttribute("Count", "2");
            items.AppendChild(orderItem2);

            XmlElement orderItem3 = doc.CreateElement("OrderItems");
            orderItem3.SetAttribute("Name", "手环");
            orderItem3.SetAttribute("Count", "3");
            items.AppendChild(orderItem3);

            doc.Save("D:\\books.xml");
            Console.WriteLine("保存成功");

 

上一篇:重定向页面和跳转调度的区别


下一篇:在请求中存取属性