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

推荐订阅源

美团技术团队
罗磊的独立博客
SecWiki News
SecWiki News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
IT之家
IT之家
博客园 - 聂微东
T
The Exploit Database - CXSecurity.com
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Vercel News
Vercel News
G
GRAHAM CLULEY
D
DataBreaches.Net
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园_首页
雷峰网
雷峰网
T
Tenable Blog
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
V
Visual Studio Blog
J
Java Code Geeks
博客园 - Franky
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
Recent Announcements
Recent Announcements
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
U
Unit 42
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
K
Kaspersky official blog
有赞技术团队
有赞技术团队
B
Blog
腾讯CDC

博客园 - Blaze

eVC++就是eVC++啊 - Blaze - 博客园 VC++常用数据类型及其操作详解[徐兆元] From VB.NET to C# and Back Again [By Darren Neimke and Scott Mitchell ] 请不要做浮躁的人(老文了,还是转一下,共勉) 地球上最慢的网路不在巴布里亚新几内亚和尼泊尔之间,而在中国网通和电信之间! [翻译]用TcpClient建立GPRS连接 1年零2个月零25天 我回来了 .NET的Pascal--Chrome来了! Gmail疯了?50个邀请! 检举个博客园的Bug! INI配置文件的替代品-XML配置文件的操作 Wallop下蛋送邀请。 从WebService的SessionID说起 Gmail下蛋了 udp的奇怪问题 webservice对"小"规模数据传输的效率问题 运动会痛苦经历 n个VB.Net C#代码转换工具 关于时间的加减计算
INI的替代品--XML配置文件读取与保存
Blaze · 2005-01-15 · via 博客园 - Blaze

.Net中并没有提供INI读写的托管类库,如果使用INI必须调用非托管API。有一个NINI提供了托管类库。
今天我们来实现XML配置文件读取与保存
1.集合类
         首先我们需要一个集合类来保存键和键值。它必须同时提供键名和索引两种查找键值的办法。所以我们采用 System.Collections.Specialized.NameValueCollection 类。需要注意的是这个类的键值只能是String。

Imports System.Xml
Public Class Setting
    
Inherits System.Collections.Specialized.NameValueCollection
End Class


2.XML配置文件格式
       配置文件格式我们采用app.config的格式

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    
<appSettings>
        
<add key="key1" value="value1"/>
    
</appSettings>
</configuration>


3.XML配置文件的读取

  Sub LoadSetting(ByVal FilePath As String)
        
Dim Reader As XmlTextReader
        
Try
            Reader 
= New XmlTextReader(FilePath)
            Reader.WhitespaceHandling 
= WhitespaceHandling.None '忽略所用Whitespace
            Me.Clear() '清除现有所有数据
        Catch ex As Exception
            
MsgBox("找不到XML文件" + ex.ToString)
            
Exit Sub
        
End Try
        
Try
            
While Reader.Read
                
If Reader.Name = "add" Then
                    
Dim Key, Value As String
                    Reader.MoveToAttribute(
"key")
                    Key 
= Reader.Value
                    Reader.MoveToAttribute(
"value")
                    Value 
= Reader.Value
                    Me.
Set(Key, Value)
                    Reader.MoveToElement()
                
End If
            
End While
        
Catch ex As Exception
            
MsgBox("XML文件格式错误" + ex.ToString)
            
Exit Sub
        
Finally
            Reader.Close()
        
End Try
    
End Sub


3.XML配置文件的写入

    Sub SaveSetting(ByVal FilePath As String)
        
Dim Writer As New XmlTextWriter(FilePath, System.Text.Encoding.Default)
        Writer.WriteStartDocument() 
'写入XML头
        Dim I As Integer
    Writer.WriteStartElement(
"configuration")    
Writer.WriteStartElement(
"appSettings")
        
For I = 0 To Me.Count - 1
            Writer.WriteStartElement(
"add")
            Writer.WriteStartAttribute(
"key"String.Empty)
            Writer.WriteRaw(Me.GetKey(I))
            Writer.WriteEndAttribute()
            Writer.WriteStartAttribute(
"value"String.Empty)
            Writer.WriteRaw(Me.Item(I))
            Writer.WriteEndAttribute()
            Writer.WriteEndElement()
        
Next
        Writer.WriteEndElement()
        Writer.WriteEndElement()
        Writer.Flush()
        Writer.Close()
    
End Sub

BTW:   也许你要问这些功能有何用处,是的在full framework中纯粹多余。可是.Net CF........