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

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

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

            <?xml version=”1.0″
encoding=”utf-8″?>

            <AllUsers>

             
<User RFID=”123456789″>

                
<USER_IP>192.168.0.1</USER_IP>

                
<Description>
oO¦yz</Description>

             
</User>

            </AllUsers>

            */

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

        }

 

    }

}

 

 

 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *