首页 > Windows开发 > 详细

C#读取xml文件

时间:2019-04-12 11:09:06      阅读:173      评论:0      收藏:0      [点我收藏+]
~/Config/Config.xml 文件: 

<?xml version="1.0" encoding="utf-8" ?>
<project>
<function name="Account">
  <attribute name="UserID" value="123456"></attribute>
</project>
公用类和方法:
using
System; using System.Xml; namespace Common { /// <summary> /// Summary description for XMLHelper. /// </summary> public class XMLHelper { public XMLHelper() { // // TODO: Add constructor logic here // } public static string ReadXML(string Function,string Attribute) { string returnValue = string.Empty; try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath("~/Config/Config.xml")); XmlNode xn=xmlDoc.SelectSingleNode("project"); XmlNodeList xnl=xn.ChildNodes; foreach(XmlNode xnf in xnl) { if(xnf.Attributes.GetNamedItem("name").Value.Equals(Function)) { XmlNodeList xnl2 = xnf.ChildNodes; foreach(XmlNode xnf2 in xnl2) { XmlElement xe=(XmlElement)xnf2; if(xe.GetAttribute("name").Equals(Attribute)) { returnValue = xe.GetAttribute("value"); break; } } break; } } return returnValue; } catch(Exception ex) { return ex.Message; } } } }
调用:
using Common;
string
userid= XMLHelper.ReadXML("Account", "UserID"); //userid="123456".

 

C#读取xml文件

原文:https://www.cnblogs.com/llmm/p/10694484.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!