第一步 先建立一個xml文檔 看文檔如下 也可以用代碼來寫入xml
<?xml version="1.0" encoding="gb2312"?><paramentList> <paramentEntity id="niname"> <id>{$Niname$}</id> <value>稱呼</value> </paramentEntity> <paramentEntity id="sign"> <id>{$Sign$}</id> <value>簽名</value> </paramentEntity></paramentList>
第二步 寫一個對於的實體類
using System;using System.Collections.Generic;using System.Text;namespace APlusEmail.Model{ public class ParamentEntity { private string id; public string Id { get { return id; } set { id = value; } } private string value; public string Value { get { return this.value; } set { this.value = value; } } }}
第三步 寫一個工具類 來操作
using System;using System.Collections.Generic;using System.Text;using APlusEmail.Model;using System.Xml;namespace APlusEmail.Common{ public static class XMLHelper { public static List<ParamentEntity> GetAllParament(string xmlPath) { List<ParamentEntity> paramentList = new List<ParamentEntity>(); //建立xmldoc對象 XmlDocument xmlDoc = new XmlDocument(); //裝載xml xmlDoc.Load(xmlPath); //擷取xml的節點 XmlNode xn = xmlDoc.SelectSingleNode("paramentList"); //擷取該節點的所有子節點 XmlNodeList xnl=xn.ChildNodes; //遍曆 foreach(XmlNode xnf in xnl) { XmlElement xe=(XmlElement)xnf; //擷取屬性 //Console.WriteLine(xe.GetAttribute("genre")); //顯示內容值 //Console.WriteLine(xe.GetAttribute("ISBN")); //遍曆裡面的某一個節點 XmlNodeList xnf1=xe.ChildNodes; ParamentEntity parementEntity = new ParamentEntity(); parementEntity.Id = xnf1[0].InnerText; parementEntity.Value = xnf1[1].InnerText; // Console.WriteLine(xn2.InnerText);//顯示子節點點文本 paramentList.Add(parementEntity); } return paramentList; } }}
這樣就返回一個對應的實體列表了 就可以供使用了