2008年6月16日 星期一

C# XML操作範例

<?xml version="1.0" encoding="UTF-8" ?>

<travianBOT>

         <settinginfo server="伺服器名稱" account="帳號" password="密碼" delay="20000" />

    </travianBOT>

 

 

Xml讀取:

 

            openFileDialog1.Filter = "XML設定檔|*.xml";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)

            {

                XmlDocument document = new XmlDocument();

                document.Load(openFileDialog1.FileName);

                try

                {

                    textBox_ServerName.Text = document.SelectSingleNode("/travianBOT/settinginfo").Attributes["server"].Value;

                    textBox_Account.Text = document.SelectSingleNode("/travianBOT/settinginfo").Attributes["account"].Value;

                    textBox_Password.Text = document.SelectSingleNode("/travianBOT/settinginfo").Attributes["password"].Value;

                    textBox_timerInterval.Text = document.SelectSingleNode("/travianBOT/settinginfo").Attributes["delay"].Value;

                }

                catch (Exception ee)

                {

                    MessageBox.Show("讀取失敗!可能是資料格式不完整~ 如果有中文資訊請確認儲存編碼為UTF8");

                    label_info.Text = ee.ToString();

                }

            }

 

Xml寫入:

 

 

            saveFileDialog1.Filter = "XML設定檔|*.xml";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)

            {

                XmlDocument document = new XmlDocument();

                document.AppendChild(document.CreateXmlDeclaration("1.0", "UTF-8", ""));//將宣告節點加入document

                XmlNode xmlnode_travianBOT = document.CreateNode(XmlNodeType.Element, "travianBOT", "");

                XmlNode xmlnode_settinginfo = document.CreateNode(XmlNodeType.Element, "settinginfo", "");

                XmlAttribute xmlattribute_server = document.CreateAttribute("server");

                XmlAttribute xmlattribute_account = document.CreateAttribute("account");

                XmlAttribute xmlattribute_password = document.CreateAttribute("password");

                XmlAttribute xmlattribute_delay = document.CreateAttribute("delay");

                xmlattribute_server.Value = textBox_ServerName.Text;

                xmlattribute_account.Value = textBox_Account.Text;

                xmlattribute_password.Value = textBox_Password.Text;

                xmlattribute_delay.Value = textBox_timerInterval.Text;

 

                xmlnode_settinginfo.Attributes.Append(xmlattribute_server);//將屬性加入xmlnode_settinginfo節點下

                xmlnode_settinginfo.Attributes.Append(xmlattribute_account);//將屬性加入xmlnode_settinginfo節點下

                xmlnode_settinginfo.Attributes.Append(xmlattribute_password);//將屬性加入xmlnode_settinginfo節點下

                xmlnode_settinginfo.Attributes.Append(xmlattribute_delay);//將屬性加入xmlnode_settinginfo節點下

                xmlnode_travianBOT.AppendChild(xmlnode_settinginfo);//xmlnode_settinginfo節點加入xmlnode_travianBOT節點下

                document.AppendChild(xmlnode_travianBOT); //xmlnode_travianBOT節點加入document

                document.Save(saveFileDialog1.FileName);

            }

 

4 意見:

張貼留言
詹姆士 提到...

請問一下要怎麼作才有辦法插入多筆資料啊?
謝謝

宅之力 提到...

外面包一層foreach迴圈就好阿
裡面做的事情都是一樣的

匿名 提到...

delay="20000"
你好 如果我想要把20000存到陣列裡面
我該怎麼寫??

匿名 提到...

太感謝你了~~~我試了好多次都沒有辦法另存
修改你提供的code就OK了~~~