惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
GbyAI
GbyAI
人人都是产品经理
人人都是产品经理
T
Tor Project blog
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
爱范儿
爱范儿
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
Vercel News
Vercel News
美团技术团队
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
L
LangChain Blog
IT之家
IT之家
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
Hacker News: Ask HN
Hacker News: Ask HN
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
量子位
Project Zero
Project Zero
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 聂微东
T
Tenable Blog
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
Microsoft Azure Blog
Microsoft Azure Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 叶小钗
Know Your Adversary
Know Your Adversary
L
Lohrmann on Cybersecurity
C
Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
I
InfoQ
P
Privacy & Cybersecurity Law Blog
Spread Privacy
Spread Privacy
Martin Fowler
Martin Fowler
腾讯CDC
S
Security @ Cisco Blogs
F
Fortinet All Blogs

博客园 - jerreychen

常用的JavaScript验证正则表达式 用CSS设置打印换页 使用Form认证实现用户登录 (LoginView的使用) 向容器(PlaceHolder)中动态添加多个用户控件(UserControl) C#.Net中WinForm采用Active Directory进行身份认证 Windows XP 如何设置系统自动关机任务 - jerreychen DataList嵌套GridView实现文章分类列表显示(2) visual studio 2008 sp1中如何让WebBrowser控件可编辑 - jerreychen SQLite 数据库初学 学习笔记 - jerreychen - 博客园 如何将.xsd文件自动生成对象 ASP.NET页面事件:顺序与回传详解 Umbraco 设置Document Types 的默认值 格式化数据 Net支持gzip 压缩格式 压缩与解压 .net 序列化数据对象 .net 下,日期的格式化 太久太久没来了,久违了-博客园 showModelDialog 关闭后改变GridView某个单元格的值
读取自定义的config文件 - jerreychen - 博客园
jerreychen · 2008-05-20 · via 博客园 - jerreychen

我们都知道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
        }