用反射机制读取XML文件

需注意的是xml文件需要把生成操作设置为嵌入的资源的资源,其他的见代码
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Xml;
  9. using System.Reflection;
  10. using System.IO;
  11. namespace XMLtest
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.         XmlDocument doc;
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             ReadDictionaryTableFromResourceXML("XMLtest.test.xml");
  23.             if (doc == null)
  24.                 return;
  25.             MessageBox.Show(GetRelationType(0));
  26.         }
  27.         public string GetRelationType(int nValue)
  28.         {
  29.             //不存在文件
  30.             string strSel = "//中小电脑钥匙调试器/自学数据/智能解锁关联/选项";
  31.             return ReadXml(strSel, nValue);
  32.         }
  33.         public XmlDocument ReadDictionaryTableFromResourceXML(string fileName)
  34.         {
  35.             try
  36.             {
  37.               //  System.Reflection.Assembly assembly = typeof(Form1).Assembly;    
  38.                 Assembly assembly = Assembly.GetExecutingAssembly();
  39.                 if (assembly != null)
  40.                 {
  41.                     Stream xmlStream = assembly.GetManifestResourceStream(fileName);
  42.                     if (xmlStream != null)
  43.                     {
  44.                         doc = new XmlDocument();
  45.                         doc.Load(xmlStream);
  46.                         return doc;
  47.                     }
  48.                 }
  49.             }
  50.             catch (Exception)
  51.             {
  52.             }
  53.             return null;
  54.         }
  55.                 /// <summary>
  56.         /// 读取xml文件
  57.         /// </summary>
  58.         /// <param name="strSel">查询语句</param>
  59.         /// <param name="nValue">查询的值</param>
  60.         /// <returns></returns>
  61.         private string ReadXml(string strSel, int nValue)
  62.         {
  63.             string str = "";
  64.             bool IsFind = false;
  65.             str = String.Format("{0:X2}:", nValue);
  66.             XmlNodeList lstNode = doc.SelectNodes(@strSel);
  67.             foreach (XmlNode node in lstNode)
  68.             {
  69.                 XmlNode nodeSelName = node.SelectSingleNode("名称");
  70.                 XmlNode nodeSelValue = node.SelectSingleNode("值");
  71.                 if (nodeSelName != null && nodeSelValue != null)
  72.                 {
  73.                     if (nodeSelValue.InnerText == nValue.ToString())
  74.                     {
  75.                         str += nodeSelName.InnerText;
  76.                         IsFind = true;
  77.                         break;
  78.                     }
  79.                 }
  80.             }
  81.             if (!IsFind)
  82.             {
  83.                 str = String.Format("{0:X2}", nValue);
  84.             }
  85.             return str;
  86.         }
  87.     }
  88. }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值