xml 檔案的建立和讀取代碼_實用技巧

來源:互聯網
上載者:User
xml檔案:
複製代碼 代碼如下:

<?xml version="1.0" encoding="utf-8" ?> <!-- 除了這一行,其他的都是動手敲入的-->
<CashAcceptType>
<type>
<name>正常收費</name>
<class>CashNormal</class>
<para></para>
</type>
<type>
<name>滿300返100</name>
<class>CashReturn</class>
<para>300,100</para>
</type>
<type>
<name>滿200返50</name>
<class>CashReturn</class>
<para>200,50</para>
</type>
<type>
<name>打8折</name>
<class>CashRebate</class>
<para>0.8</para>
</type>
<type>
<name>打7折</name>
<class>CashRebate</class>
<para>0.7</para>
</type>
</CashAcceptType>

讀取.xml檔案:
複製代碼 代碼如下:

DataSet ds = new DataSet();
//根據路徑讀取.xml檔案到DataSet
ds.ReadXml(Application.StartupPath + "\\CashAcceptType.xml");
//將讀取到的記錄綁定到下拉式清單方塊中
foreach (DataRowView dr in ds.Tables[0].DefaultView)
{
cbxType.Items.Add(dr["name"].ToString());//dr["name"]擷取.xml檔案中每個type對應的name那一項的值,即正常收費,滿300減100,滿200減50等
}
//根據使用者的選項,查詢使用者選擇項的相關行,(即根據給定的name找到該name對應的其他參數)
DataRow dr = ((DataRow[])ds.Tables[0].Select("name='" + cbxType.SelectedItem.ToString() + "'"))[0];
dr["class"]找到name對應的xml的class
dr["para"]找到para

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.