search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

C# XML(1) 建立/修改/查詢 – jashliao部落格

C#  XML(1) 建立/修改/查詢


 

using System;

using
System.Collections.Generic;

using
System.Linq;

using
System.Text;

using System.Xml;

 

namespace
CS_Console_XML

{

    class Program

    {

        static void Pause()

        {

            Console.Write(“Press any key to continue . . . “);

            Console.ReadKey(true);

        }

        static void Main(string[]
args)

        {

            //aÂAODsXMLE¡ÑMgJ

            /*

            //oOeDXg2GG

             encoding=”utf-8″?>

           

             

                
192.168.0.1

                

oO¦yz

             

           

            */

            XmlTextWriter
XTW = new XmlTextWriter(“user.xml”, Encoding.UTF8);

            XTW.WriteStartDocument();

 

            XTW.WriteStartElement(“AllUsers”);

            XTW.WriteStartElement(“User”);

            XTW.WriteAttributeString(“RFID”, “123456789”);

 

            XTW.WriteElementString(“USER_IP”, “192.168.0.1”);

            XTW.WriteElementString(“Description”, oO¦yz”);

            XTW.WriteEndElement();

            XTW.WriteEndElement();

 

            XTW.Flush();     //goa~P|gJE¡Ñ

            XTW.Close();

           

            //aÂAaLu

            XmlDocument
xd = new XmlDocument();

            xd.Load(“user.xml”);

            XmlNode
root = xd.SelectSingleNode(“//User”);

            foreach
(XmlElement elm in
root.ChildNodes)

            {

                Console.WriteLine(elm.Name.Trim()
+
“:” + elm.InnerText.Trim());//Node
WU:NodeE

            }

 

            //¡Ñ±i

            XmlDocument
xmlDoc = new XmlDocument();

            xmlDoc.Load(“user.xml”);

            XmlNode
root1 = xmlDoc.SelectSingleNode(“//User”);

            foreach (XmlElement elm in
root1.ChildNodes)

            {

                elm.InnerText = “123”;

            }

            //xmlDoc.Save(“user.xml”);//¡Ñ±i

            xmlDoc.Save(“user1.xml”);//DtsC

            Pause();

        }

 

    }

}

 

 

 




熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦