ASP.NET读取XML文件
一.首先介绍xml 代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8" ?> <root> <company id="6b357589b12f6141fc48c4b0375ef2f9"> <Wordkey id="5f499e85ed57a5af01fdc0ab05d8b3d2"> 广州 </Wordkey> </company> <company id="8ebd5d853db2ee913b3c1bf685741b52"> <Wordkey id="77bf607cefce3aea33913505dca3bfdb"> 深圳 </Wordkey> </company> <!--全部--> <company id="a8b0c20416853bda54120bf19477ad11"> <Wordkey id="1e5142e81a34260029e8dc84c21d00d3"> 北京,上海,广州,深圳,珠海,东莞,青岛,连云港市 </Wordkey> </company> </root>
二.读取文件:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class NewWebsiteHomeLogic { #region 当前类的实例 private static readonly NewWebsiteHomeLogic logicInstances = new NewWebsiteHomeLogic(); private NewWebsiteHomeLogic() { } public static NewWebsiteHomeLogic GetInstances() { return logicInstances; } #endregion #region 获取关键字的列表 /// <summary> /// 获取关键字的列表 /// </summary> /// <param name="companyID">用户所属公司ID</param> /// <returns></returns> public string[] GetDependencyList(string companyID) { return GetDependencyList(companyID, string.Empty); } #endregion #region 获取关键字的列表 /// <summary> ///获取关键字的列表 /// </summary> /// <param name="companyID">用户所属公司id</param> /// <param name="wordkeyID">关键字ID</param> /// <returns></returns> public string[] GetDependencyList(string companyID, string wordkeyID) { if (string.IsNullOrEmpty(companyID)) { return null; } XmlDocument doc = GetDocument(); XmlNode node = GetNode(companyID, wordkeyID, doc); if (string.IsNullOrEmpty(node.InnerText) || node == null) { return null; } var xmlNodeText = node.InnerText; xmlNodeText = xmlNodeText.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace(" ", string.Empty); xmlNodeText = Core.Utility.FilterRepeatItem(xmlNodeText, new char[] { ',' }); var array = xmlNodeText.Split(new char[] { ',' }); return array; } #endregion #region 获取xml节点 private XmlNode GetNode(string companyID, string wordkeyID, XmlDocument doc) { if (string.IsNullOrEmpty(companyID) || doc == null) { return null; } XmlNode node = null; string xpath = "/root/company[@id='" + companyID + "']"; node = doc.SelectSingleNode(xpath); if (node == null || node.HasChildNodes == false) { return null; } if (string.IsNullOrEmpty(wordkeyID)) { xpath += "/Wordkey[@id='0']"; } else { xpath += "/Wordkey[@id='" + wordkeyID + "']"; } if ((node == null || string.IsNullOrEmpty(node.InnerText)) && string.IsNullOrEmpty(wordkeyID) == false) { return GetNode(companyID, string.Empty, doc); } return node; } #endregion #region 获取xml文件 private XmlDocument GetDocument() { XmlDocument doc = null; string path = System.Web.HttpContext.Current.Server.MapPath("~/config/NewWebsiteHome.xml"); if (string.IsNullOrEmpty(path)) { return null; } else { doc = new XmlDocument(); doc.Load(path); } return doc; } #endregion }
三.获取数据
string[] strdepens = NewWebsiteHomeLogic.GetInstances().GetDependencyList(companyID, wordkeyID); string words = ""; if (strdepens.Count() > 0) { int count = strdepens.Count(); for (int i = 0; i < count; i++) { if (i < count - 1) { words += (strdepens[i] + "|"); } else { words += (strdepens[i]); } } } }
凡事不平凡,做好一件事就好