需注意的是xml文件需要把生成操作设置为嵌入的资源的资源,其他的见代码
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Xml;
- using System.Reflection;
- using System.IO;
- namespace XMLtest
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- XmlDocument doc;
- private void button1_Click(object sender, EventArgs e)
- {
- ReadDictionaryTableFromResourceXML("XMLtest.test.xml");
- if (doc == null)
- return;
- MessageBox.Show(GetRelationType(0));
- }
- public string GetRelationType(int nValue)
- {
- //不存在文件
- string strSel = "//中小电脑钥匙调试器/自学数据/智能解锁关联/选项";
- return ReadXml(strSel, nValue);
- }
- public XmlDocument ReadDictionaryTableFromResourceXML(string fileName)
- {
- try
- {
- // System.Reflection.Assembly assembly = typeof(Form1).Assembly;
- Assembly assembly = Assembly.GetExecutingAssembly();
- if (assembly != null)
- {
- Stream xmlStream = assembly.GetManifestResourceStream(fileName);
- if (xmlStream != null)
- {
- doc = new XmlDocument();
- doc.Load(xmlStream);
- return doc;
- }
- }
- }
- catch (Exception)
- {
- }
- return null;
- }
- /// <summary>
- /// 读取xml文件
- /// </summary>
- /// <param name="strSel">查询语句</param>
- /// <param name="nValue">查询的值</param>
- /// <returns></returns>
- private string ReadXml(string strSel, int nValue)
- {
- string str = "";
- bool IsFind = false;
- str = String.Format("{0:X2}:", nValue);
- XmlNodeList lstNode = doc.SelectNodes(@strSel);
- foreach (XmlNode node in lstNode)
- {
- XmlNode nodeSelName = node.SelectSingleNode("名称");
- XmlNode nodeSelValue = node.SelectSingleNode("值");
- if (nodeSelName != null && nodeSelValue != null)
- {
- if (nodeSelValue.InnerText == nValue.ToString())
- {
- str += nodeSelName.InnerText;
- IsFind = true;
- break;
- }
- }
- }
- if (!IsFind)
- {
- str = String.Format("{0:X2}", nValue);
- }
- return str;
- }
- }
- }