使用C#讀取XML節點,修改XML節點

來源:互聯網
上載者:User

標籤:

例子:

<add key="IsEmptyDGAddRootText" value="" />    <!--是否可以修改歸檔狀態檔案 false: 不能修改 true 可以修改-->    <add key="IsEnableEditArchiveEntry" value="true" />    <!--是否輸出調用日誌-->    <add key="IsEnableOutPutLog" value="true" />    <!--是否調試狀態-->    <add key="IsDebug" value="true" />    <!--調試狀態使用者Id-->    <add key="DebugUserId" value="dajkTest1" />    <!--調試狀態使用者密碼-->    <add key="DebugUserPwd" value="dajkTest1***" />

做法:

 XmlDocument xdoc = new XmlDocument();    //建立XML執行個體                if (File.Exists("web.config"))                {
            //如果檔案存在就操作 xdoc.Load("web.config"); XmlNodeList adds = xdoc.SelectNodes("//appSettings/add");
            //建立讀取節點的方法並且找到對應節點(appSettings下的add子節點)

            if (this.rbEntryStateY.Checked) {
              //找到對應第11個節點的第一個屬性並且修改值                        adds[11].Attributes[1].Value = "true";                    }                    else if (this.rbEntryStateN.Checked)                    {                        adds[11].Attributes[1].Value = "false";                    }                    if (this.rbLogY.Checked)                    {                        adds[12].Attributes[1].Value = "true";                    }                    else if (this.rbLogN.Checked)                    {                        adds[12].Attributes[1].Value = "false";                    }                    if (this.rbDebugStateY.Checked)                    {                        adds[13].Attributes[1].Value = "true";                    }                    else if (this.rbDebugStateN.Checked)                    {                        adds[13].Attributes[1].Value = "false";                    }                    adds[14].Attributes[1].Value = DebugUserId;                    adds[15].Attributes[1].Value = DebugUserPwd;            //全部修改完畢,儲存XML檔案                    xdoc.Save("web.config");                }

 

使用C#讀取XML節點,修改XML節點

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.