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

推荐订阅源

博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
S
SegmentFault 最新的问题
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
DataBreaches.Net
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
D
Docker
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
宝玉的分享
宝玉的分享
腾讯CDC
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
T
The Exploit Database - CXSecurity.com
T
The Blog of Author Tim Ferriss
V
V2EX
S
Securelist
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Y
Y Combinator Blog
P
Proofpoint News Feed
T
Tor Project blog
AWS News Blog
AWS News Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
T
Threat Research - Cisco Blogs
B
Blog
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
N
News and Events Feed by Topic
博客园 - 司徒正美
H
Help Net Security
C
Cisco Blogs
C
Check Point Blog
S
Secure Thoughts

博客园 - 感觉De味道

CRC16_Check 源代码类修改版 HI baidu~~~~~~开通了~~~ [转] js 选时间的控件 - 感觉De味道 自我复制的简单实现(C#) 使用C#调用外部命令 - 感觉De味道 - 博客园 VS2005 中比较有用的快捷键 C#让windows程序只运行一次 [转] - 感觉De味道 - 博客园 只允许一个进程运行的实例 网址大全[收集网上大部份好的开源网] C#模拟MSN窗体抖动 javascript 常用小技巧 - 感觉De味道 - 博客园 衔接UI线程和管理后台工作线程的类(多线程、异步调用)[转] 关于线程问题 [转] 扫雷高手版出台 (本站原创) 用C#捕捉键盘和鼠标 类型转换(本站原创) 一个简单的C#托盘程序(本站原创) 232串口通信程序刚完成(本站原创) 木马代码
写了一个操作XML文件的类
感觉De味道 · 2007-06-05 · via 博客园 - 感觉De味道

 一个操作XML文件的类。。部份功能在完善中~~~~

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
/*
 * 作者 By感觉De味道;
 * 版板 SghComm1.0
 * 联系方式:ZhouFLeru@hotmail.com
 */
namespace TextCom
{
    class SettingXml
    {
        private static System.Xml.XmlDocument xDoc = null;//加载配置文件用到;
        //生成配置文件;
        public static void NewSeeting()
        {
            string[] addkey ={ "Port", "BaudRate", "DataBits", "Parity", "StopBits" };
            string[] addvalue ={ "1", "38400", "8", "0", "1" };
            XmlDocument xmldoc;//XMl文档
            XmlNode xmlnode;//XMl 中的节点
            XmlElement xmlelem; //XML中元素
             xmldoc = new XmlDocument();
            XmlDeclaration xmlDec = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
            //加入XML的声明段落
            xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmldoc.AppendChild(xmlnode);
            //加入一个根元素
            xmlelem = xmldoc.CreateElement("", "configuration", "");
            xmldoc.AppendChild(xmlelem);
            //加入另外一个元素
            for (int i = 0; i < 5; i++)
            {
                XmlNode root = xmldoc.SelectSingleNode("configuration");//查找<configuration>
                XmlElement xe1 = xmldoc.CreateElement("add");//创建一个<add>节点
                xe1.SetAttribute("key", addkey[i] + "");
                xe1.SetAttribute("value", addvalue[i] + "");
                root.AppendChild(xe1);//添加到<configuration>节点中
            }
            //保存创建好的XML文档
            xmldoc.Save("setting.config");
        }
        //Load配置文件;
        public static string LoadSettingXml(string Appkey)
        {
            string path = "";
            XmlNode LNode;
            XmlElement LElem;

            try {
                path = "setting.config";
           
            if (xDoc==null)
            {
                xDoc=new System.Xml.XmlDocument();
                xDoc.Load(path);
            }
        }catch{throw new Exception("未发现配置文件!");}
            try{

                LNode = xDoc.SelectSingleNode("//configuration");
                LElem = (XmlElement)LNode.SelectSingleNode("//add[@key='" + Appkey + "']");
                if (LElem != null)
                    return LElem.GetAttribute("value");
                else
                    return "";
            }
            catch
            {
                throw new Exception("Load配置文件错误!");
            }
            }
    }
}
-----------------------------------------------------

成生如下结构:

<?xml version="1.0"?>
<configuration>
  <add key="Port" value="1" />
  <add key="BaudRate" value="38400" />
  <add key="DataBits" value="8" />
  <add key="Parity" value="0" />
  <add key="StopBits" value="1" />
</configuration>