读取自定义的config文件
2009-03-27 10:18
288 查看
我们都知道web.config 的读取方法有两种
1、使用System.Configuration 的 ConfigurationManager 读取
ConfigurationManager.AppSettings["key"].ToString();
2、使用AppSettingReader进行读取
AppSettingsReader reader = new AppSettingsReader();
reader.GetValue("key", typeof(string)) as string;
很多时候我们需要自己定义些Config文件,这些需要怎么去读呢?
首先我们新建一个Config文件,命名为Test.config,当然里面的内容都是不需要的,因为我们要自己定义,所以把它们都删除掉
接着写我们自己的内容(哦,对了,config文件事实上就是一个XML文件,所以第一句声明必须有的)
<?xml version="1.0" ?>
<templates>
<template templateID="welcome_Template">
<subject id="sd" name="top">
<![CDATA[
##ContentTitle## has ##ContentAction##
]]>
</subject>
<bodyHtml>
<![CDATA[
<div>
<p>
Content Title : ##ContentTitle##
</p>
<p>
If the link is blocked, please copy the url below to view detail.<br />
URL: http://##ContentDocumentGUID##
</p>
<p style="text-align:right; padding:10px;">
Best Regards.
</p>
</div>
]]>
</bodyHtml>
</template>
</templates>
好了,上面就是我们自己写的config文件了,接下来就要进行读取操作了
XmlTextReader reader = new XmlTextReader(Server.MapPath("Template.config")); // new一个XMLTextReader实例
XmlDocument doc = new XmlDocument();
doc.Load(reader);//
reader.Close();//关闭reader,不然config文件就变成只读的了
XmlNodeList nodeList = doc.SelectSingleNode("/templates").ChildNodes;
foreach (XmlNode n in nodeList)
{
//XmlNode 的Attributes属性是列出这个Node的所以属性,比如我们定义的template有个属性叫templateID
if (n.Attributes["templateID"].Value == "welcome_Template")
{
// 再遍历取这个node的子node
foreach (XmlNode node in n.ChildNodes)
Response.Write(node.Name + node.InnerText + "<hr />");
}
}
嗯,这样就可以读取我们自己写的config文件了
还有一点,对于每个XmlNode,如果我们要取得它的所有属性可以用下面的代码
XmlAttributeCollection xmlAttrList = node.Attributes;
foreach (XmlAttribute attr in xmlAttrList)
{
// Action
}
1、使用System.Configuration 的 ConfigurationManager 读取
ConfigurationManager.AppSettings["key"].ToString();
2、使用AppSettingReader进行读取
AppSettingsReader reader = new AppSettingsReader();
reader.GetValue("key", typeof(string)) as string;
很多时候我们需要自己定义些Config文件,这些需要怎么去读呢?
首先我们新建一个Config文件,命名为Test.config,当然里面的内容都是不需要的,因为我们要自己定义,所以把它们都删除掉
接着写我们自己的内容(哦,对了,config文件事实上就是一个XML文件,所以第一句声明必须有的)
<?xml version="1.0" ?>
<templates>
<template templateID="welcome_Template">
<subject id="sd" name="top">
<![CDATA[
##ContentTitle## has ##ContentAction##
]]>
</subject>
<bodyHtml>
<![CDATA[
<div>
<p>
Content Title : ##ContentTitle##
</p>
<p>
If the link is blocked, please copy the url below to view detail.<br />
URL: http://##ContentDocumentGUID##
</p>
<p style="text-align:right; padding:10px;">
Best Regards.
</p>
</div>
]]>
</bodyHtml>
</template>
</templates>
好了,上面就是我们自己写的config文件了,接下来就要进行读取操作了
XmlTextReader reader = new XmlTextReader(Server.MapPath("Template.config")); // new一个XMLTextReader实例
XmlDocument doc = new XmlDocument();
doc.Load(reader);//
reader.Close();//关闭reader,不然config文件就变成只读的了
XmlNodeList nodeList = doc.SelectSingleNode("/templates").ChildNodes;
foreach (XmlNode n in nodeList)
{
//XmlNode 的Attributes属性是列出这个Node的所以属性,比如我们定义的template有个属性叫templateID
if (n.Attributes["templateID"].Value == "welcome_Template")
{
// 再遍历取这个node的子node
foreach (XmlNode node in n.ChildNodes)
Response.Write(node.Name + node.InnerText + "<hr />");
}
}
嗯,这样就可以读取我们自己写的config文件了
还有一点,对于每个XmlNode,如果我们要取得它的所有属性可以用下面的代码
XmlAttributeCollection xmlAttrList = node.Attributes;
foreach (XmlAttribute attr in xmlAttrList)
{
// Action
}
相关文章推荐
- .net 中读取自定义Config文件
- .net 中读取自定义Config文件
- C# WinForm中如何自定义config文件(XML文件),并且读取和保存它
- Asp.net 自定义config文件读取
- ASP.NET读取自定义的config文件
- C# config配置文件 自定义节点读取
- .Net 自定义读取自定Config文件
- .net 中读取自定义Config文件
- 读取自定义的config文件
- C#中读取自定义的config文件(数据库的连接符大多情况使用)
- 读取app.config文件中的自定义内容
- web.config中自定义配置项读取
- 从配置文件中读取自定义扩展类的模式
- C# 读取保存App.config配置文件的完整源码参考
- Silverlight读取Web.config配置文件
- Winform读取app.config文件
- Spring-boot中读取config配置文件的两种方式
- 来段代码提提神-Java读取config.properties文件中的key=value
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件