C#讀取XML(全部或根據某個節點的屬性值)
阿新 • • 發佈:2019-02-15
<?xml version="1.0" encoding="utf-8" ?> <config> <app AppID="6000"> <Name></Name> <PackageName></PackageName> <LoadUrl></LoadUrl> <Version>1.2.130923</Version> <Introduction> </Introduction> <Type>1</Type> </app> <app AppID="6001"> <Name></Name> <PackageName></PackageName> <LoadUrl></LoadUrl> <Version>1.2.130923</Version> <Introduction> </Introduction> <Type>1</Type> </app> </config>
#region 應用列表 /// <summary> /// 獲取應用列表 /// </summary> /// <param name="deviceType">作業系統及作業系統版本,比如iPhone OS6.1.3</param> /// <returns>JSON格式的應用列表字串</returns> public static string GetAppList(string deviceType) { string deviceOS = appTool.GetDeviceOS(deviceType); appResult<appInfo> result = new appResult<appInfo>(); try { string configbasePath = appTool.GetBasePath(deviceOS); string configPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configbasePath); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(configPath); XmlNode xn = xmlDoc.SelectSingleNode("config"); XmlNodeList xnl = xn.ChildNodes; List<appInfo> data = new List<appInfo>(); foreach (XmlNode singleXmlNode in xnl) { appInfo singleAppInfo = new appInfo(); string AppID = singleXmlNode.Attributes["AppID"].Value; singleAppInfo.AppID = int.Parse(AppID); singleAppInfo.Name = singleXmlNode.SelectSingleNode("Name").InnerText; singleAppInfo.PackageName = singleXmlNode.SelectSingleNode("PackageName").InnerText; singleAppInfo.LoadUrl = singleXmlNode.SelectSingleNode("LoadUrl").InnerText; string iconbasePath = "img/icon/" + AppID + "/" + deviceOS; string iconPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, iconbasePath); DirectoryInfo iconDI = new DirectoryInfo(iconPath); FileInfo[] iconfiles = iconDI.GetFiles(); string IconUrl = string.Empty; foreach (FileInfo file in iconfiles) { IconUrl = "http://" + System.Web.HttpContext.Current.Request.Url.Host + ":" + System.Web.HttpContext.Current.Request.Url.Port.ToString() + "/store/" + iconbasePath + "/" + file.Name; } singleAppInfo.IconUrl = IconUrl; singleAppInfo.Version = singleXmlNode.SelectSingleNode("Version").InnerText; singleAppInfo.Introduction = singleXmlNode.SelectSingleNode("Introduction").InnerText; singleAppInfo.Type = int.Parse(singleXmlNode.SelectSingleNode("Type").InnerText); string overviewbasePath = "img/overview/" + AppID + "/" + deviceOS; string overviewPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, overviewbasePath); DirectoryInfo DI = new DirectoryInfo(overviewPath); FileInfo[] files = DI.GetFiles(); List<string> overviews = new List<string>(); foreach (FileInfo file in files) { string overviewUrl = "http://" + System.Web.HttpContext.Current.Request.Url.Host + ":" + System.Web.HttpContext.Current.Request.Url.Port.ToString() + "/store/" + overviewbasePath + "/" + file.Name; overviews.Add(overviewUrl); } singleAppInfo.Overviews = overviews; data.Add(singleAppInfo); } result.Data = data; result.Result = 1; result.Message = ""; result.TotalCount = xnl.Count; return JsonHelper.JSSerialize(result); } catch (Exception ex) { result.Result = -1; result.Message = ex.Message; result.TotalCount = 0; return JsonHelper.JSSerialize(result); } } #endregion #region /// <summary> /// 獲取指定AppID的LoadUrl /// </summary> /// <param name="deviceType">作業系統及作業系統版本,比如iPhone OS6.1.3</param> /// <param name="AppID">應用ID</param> /// <returns>應用的Url</returns> public static string GetMappingUrl(string deviceType, string AppID) { string mappingUrl = string.Empty; string deviceOS = appTool.GetDeviceOS(deviceType); appResult<appInfo> result = new appResult<appInfo>(); try { string configbasePath = appTool.GetBasePath(deviceOS); string configPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configbasePath); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(configPath); //根據指定AppID屬性獲取該應用的LoadUrl,請留意這種篩選寫法 XmlNode xnP = xmlDoc.SelectSingleNode("config/app[@AppID='" + AppID + "']/LoadUrl"); mappingUrl = xnP.InnerText; } catch (Exception ex) { } return mappingUrl; } #endregion