最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#如何實(shí)現(xiàn)讀取xml文件內(nèi)的數(shù)據(jù)

 更新時(shí)間:2025年08月28日 08:48:40   作者:ccut 第一混  
C#項(xiàng)目常用XML存儲(chǔ)參數(shù),通過(guò)字典、XmlDocument加載、XPath定位節(jié)點(diǎn)讀取數(shù)據(jù),實(shí)現(xiàn)配置管理,代碼示例適用于WinForms,便于維護(hù)

c# 讀取xml文件內(nèi)的數(shù)據(jù)

好多大型的項(xiàng)目,把一些固定的參數(shù)都存在 xml文件里。

  • 創(chuàng)建c# winfom 項(xiàng)目,test_xml
  • 創(chuàng)建resources文件夾存放xml文件

  • 創(chuàng)建parameters.xml文件

<root>
    <test_xml>
        <param name ="threshold" value ="128"/>
        <param name ="sum_max" value ="100"/>
        <param name ="sum_min" value ="50"/>
        <param name ="ratio" value ="0.75"/>
        <param name ="img_path" value ="C:\\Users\\86957\\Pictures\\CCD\\0243480-20240326103539.jpg"/>
    </test_xml>
</root>


  • 根目錄root
  • 項(xiàng)目目錄test_xml
  • param+內(nèi)容

c# 讀取xml文件內(nèi)的數(shù)據(jù)的方法

A創(chuàng)建字典用于存放數(shù)據(jù):

Dictionary<string, string> Params = new Dictionary<string, string>();

B加載文件:

XmlDocument presentxml = new XmlDocument();
presentxml.Load(FileName);

(XmlDocument屬于System.Xml命名空間,是XML文檔的內(nèi)存表示)

C定位:

XmlNodeList paramNodes = presentxml.SelectNodes("/root/test_xml/param"); 

(XmlNodeList表示通過(guò)XPath查詢(xún)返回的節(jié)點(diǎn)集合)

D 讀取并寫(xiě)入字典:

            foreach (XmlNode node in paramNodes)
            {
                string name = node.Attributes["name"].Value;  // 獲取name屬性
                string value = node.Attributes["value"].Value; // 獲取value屬性
                Params.Add(name, value); // 添加到字典
            }

(XmlNode表示XML文檔中的單個(gè)節(jié)點(diǎn)(基類(lèi)))

完整代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace test_xml
{
    public partial class Form1 : Form
    {
        //string xmldata_path = "D:\\VS\\works\\test_xml\\test_xml\\resources\\parameters.xml";//絕對(duì)
        string xmldata_path = "../../resources/parameters.xml";//相對(duì)
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Dictionary<string, string> Params = new Dictionary<string, string>(ParamXML.ReadParamXML(xmldata_path));

            textBox1.AppendText(Params["threshold"]+"\r\n");
            textBox1.AppendText(Params["img_path"]+"\r\n");
            double difference = (int.Parse(Params["sum_max"]) - int.Parse(Params["sum_min"]))* double.Parse(Params["ratio"]);
            textBox1.AppendText(difference.ToString()+"\r\n");

        }
    }



    class ParamXML
    {
        public static Dictionary<string, string> ReadParamXML(string FileName)
        {
            Dictionary<string, string> Params = new Dictionary<string, string>();
            XmlDocument presentxml = new XmlDocument();
            presentxml.Load(FileName);

            
            XmlNodeList paramNodes = presentxml.SelectNodes("/root/test_xml/param"); // 使用XPath定位節(jié)點(diǎn)
            foreach (XmlNode node in paramNodes)
            {
                string name = node.Attributes["name"].Value;  // 獲取name屬性
                string value = node.Attributes["value"].Value; // 獲取value屬性
                Params.Add(name, value); // 添加到字典
            }



            return Params;
        }

    }






}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

云阳县| 通河县| 蒙阴县| 永福县| 阆中市| 永修县| 资溪县| 卓尼县| 株洲市| 伊金霍洛旗| 丹凤县| 柏乡县| 集安市| 舒城县| 济南市| 镇沅| 延边| 泽州县| 淮南市| 威海市| 萍乡市| 疏勒县| 黄石市| 鸡西市| 禹州市| 阆中市| 兰坪| 河东区| 桂阳县| 建瓯市| 兴国县| 丰顺县| 海淀区| 左权县| 广丰县| 利津县| 镇雄县| 读书| 盱眙县| 柳州市| 重庆市|