最新国产好看的视频,伊人天堂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ù),通過字典、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表示通過XPath查詢返回的節(jié)點(diǎn)集合)

D 讀取并寫入字典:

            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)(基類))

完整代碼

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)論

武威市| 建瓯市| 静乐县| 龙山县| 阿鲁科尔沁旗| 怀仁县| 唐河县| 区。| 海安县| 额尔古纳市| 大埔县| 舟曲县| 彭阳县| 河西区| 萨迦县| 靖安县| 吉林市| 民丰县| 南皮县| 城口县| 林口县| 墨脱县| 保康县| 阳信县| 亳州市| 邛崃市| 灵石县| 禹城市| 普陀区| 琼海市| 曲周县| 万载县| 大新县| 三原县| 凤台县| 宜章县| 葵青区| 焉耆| 紫阳县| 岳阳县| 辛集市|