我也不喜欢嵌套很多层的xml,当然某些情况下这样显示起来更加的直白符合逻辑,下面用一个简单的xml来做例子,xml内容如下(名字:CreateHtml.config ):
<?xml version="1.0" encoding="utf-8" ?> <web> <website key="0" value="title"/> <website key="1" value="name"/> <website key="2" value="url"/> <website key="3" value="createDate"/> <website key="4" value="desc"/> <testAA key="fds" value="123" /> <testAA key="HH" value="123" /> <testAA key="RRE" value="123" /> <net> <aspx key="c#" value="linq" /> <aspx key="f#" value="fn" /> </net> </web>
现在我只想得到web节点下的website项怎么操作呢。只需要用XmlDocument的Load方法将某个路径下的xml文件转载进内存,然后用XmlNodeList进行筛选即可。
当然了,在读取xml文件时候我们无可避免会涉及到文件的操作,比如该路径的xml是否存在:
/// <summary> /// 检查某个文件是否存在 /// </summary> /// <param name="filePath">文件的物理路径</param> /// <returns></returns> private bool IsExistFiles(string filePath) { try { if (System.IO.File.Exists(filePath)) return true; } catch (Exception ex) { throw new Exception(ex.Message + "\r\n" + "May be other error:文件不存在或禁止访问!"); } return false; }
<div id="div_test"><%=aaaa %></div><br /><form runat="server" id="form1"> <asp:Button runat="server" ID="btnGetXml" Text="读取xml文件" onclick="btnGetXml_Click" /> </form>
using System; using System.Xml; using System.Collections.Generic;
public string aaaa = string.Empty; /// <summary> /// 读取xml文件测试 /// </summary> protected void btnGetXml_Click(object sender, EventArgs e) { Dictionary<string, string> dic = ReadConfig("~/Config/CreateHtml.config", "web/website"); if (dic == null) return; foreach (KeyValuePair<string, string> kv in dic) aaaa += "<Br />" + kv.Key + ":" + kv.Value; } ///<summary> /// 读取配置文件某节点的个数 ///</summary> ///<param name="path">配置文件的路径</param> ///<param name="nodeName">要获取的节点</param> private Dictionary<string, string> ReadConfig(string path, string nodeName) { Dictionary<string, string> dic = new Dictionary<string, string>(); string absoPath = string.Empty; //绝对路径 try { absoPath = System.Web.HttpContext.Current.Server.MapPath(path); if (IsExistFiles(absoPath)) { XmlDocument xd = new XmlDocument(); xd.Load(absoPath); XmlNodeList nodeList = xd.SelectNodes(nodeName); //得到相应节点的集合 if (nodeList != null && nodeList.Count > 0) for (int i = 0; i < nodeList.Count; i++) dic.Add(nodeList.Item(i).Attributes["key"].Value, nodeList.Item(i).Attributes["value"].Value); } return dic; } catch (Exception ex) { throw new Exception(ex.Message); } }
如果要选择testAA项,只需要修改SelectNodes对象即可(web/testAA)。
注意,我用的是Dictionary存储,字典是键值对KeyValuePair,哈希要保持键值唯一,所以你在用上面的方法时,xml里面的key不能有重复,不然会报错。解决方法,1.修改Add方法,确保唯一,有重复就不添加。
public class SafeDictionary<TKey, TValue> { private readonly object _Padlock = new object(); private readonly Dictionary<TKey, TValue> _Dictionary = new Dictionary<TKey, TValue>(); public bool ContainsKey(TKey key) { return _Dictionary.ContainsKey(key); } public TValue this[TKey key] { get { return _Dictionary[key]; } } public void Add(TKey key, TValue value) { lock(_Padlock) { _Dictionary.Add(key,value); } } }
2.换一个对象存储,比如NameValueCollection,在这就不说了。