Asp.NET Xml 初试

XmlDocument doc = new XmlDocument();



            //SelectSIngleNote and SelectNodes

            doc.Load("data.xml");

            XmlNodeList xnlist = doc.SelectNodes("books/book");

            foreach (XmlNode xn in xnlist)

            {

                Console.WriteLine(xn.OuterXml);

                XmlNode titleNode = xn.SelectSingleNode("title");

                Console.WriteLine(xn.InnerText);

                Console.WriteLine("  ");

            }



            //Add xmlNode Attribute and get attributorNote

            doc.Load("data5.xml");

            XmlNodeList xn1list = doc.SelectNodes("books/book");

            foreach (  XmlNode xn   in xn1list)

            {

                XmlAttribute xmlAttr = doc.CreateAttribute("Author");

                xmlAttr.Value = "Gzh4455";

                xn.Attributes.Append(xmlAttr);



                //XmlAttribute idAttributor = xn.Attributes["id"];

                //Console.WriteLine(idAttributor.Value);

                //Console.WriteLine(" ");

            }

            doc.Save("data5.xml");

            Console.ReadKey();



            //LoadXml and CreateNode and save xmldocument

            doc.LoadXml("<book><title>Morning</title><price>20.5</price></book>");

            XmlNode xne = doc.CreateNode("element", "pages", "");

            xne.InnerText = "290";

            XmlElement rootElement = doc.DocumentElement;



            rootElement.AppendChild(xne);

            Console.Write(doc.OuterXml);

            doc.Save("data.xml");

            Console.ReadKey();



            //doc.Load("data4.xml");

            //XmlNode xn = doc.SelectSingleNode("book/person/age");

            //xn.InnerText = "30";

            //doc.Save("data4.xml");

            //Console.WriteLine(xn.InnerText);





            //AppendChild

            doc.Load("data.xml");

            Console.WriteLine(doc.OuterXml);

            XmlNode xn2 = doc.CreateNode("element", "person", "");

            XmlNode age = doc.CreateNode("element", "age", "");

            age.InnerText = "28";

            xn2.AppendChild(age);

            doc.DocumentElement. AppendChild(xn2);

            doc.Save("data.xml");





            //XmlTextWriter writer = new XmlTextWriter("data4.xml", null);

            //writer.Formatting = Formatting.Indented;

           // doc.Save(writer);



           



            Console.ReadKey();

 

下面是一些尝试过的方法

 

你可能感兴趣的:(asp.net)