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

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - 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........