c#如何實(shí)現(xiàn)讀取xml文件內(nèi)的數(shù)據(jù)
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)文章
C#采用HttpWebRequest實(shí)現(xiàn)保持會(huì)話上傳文件到HTTP的方法
這篇文章主要介紹了C#采用HttpWebRequest實(shí)現(xiàn)保持會(huì)話上傳文件到HTTP的方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08
c# 動(dòng)態(tài)加載dll文件,并實(shí)現(xiàn)調(diào)用其中的方法(推薦)
下面小編就為大家?guī)硪黄猚# 動(dòng)態(tài)加載dll文件,并實(shí)現(xiàn)調(diào)用其中的方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
c#?復(fù)寫Equals方法的實(shí)現(xiàn)
本文主要介紹了c#?復(fù)寫Equals方法的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
WPF仿Tabcontrol實(shí)現(xiàn)切換多個(gè)不同View
這篇文章主要為大家詳細(xì)介紹了WPF如何模仿Tabcontrol實(shí)現(xiàn)切換多個(gè)不同View,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11

