XmlTextReader/XmlTextWriter读写XML文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;


namespace ConsoleApplication
{
    class XMLUtils
    {
        public string dbContection = @"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={HOST})(PORT={PORT})))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={SERVICE_NAME})));User Id={UID};Password={PWD};";
        
        public const string INFO = "info";
        public const string DATABASE = "database";
        public const string CONTECTION = "contection";

        public const string SMTP = "smtp";
        public const string HOST = "host";
        public const string PORT = "port";
        public const string USERNAME = "username";
        public const string PASSWORD = "password";

        public const string MAIL = "mail";
        public const string FROM = "from";
        public const string TO = "to";

        public const string SENDMAIL = "sendmail";
        public const string SUBJECT1 = "subject1";
        public const string BODY1 = "body1";

        public const string DATAGENERATION = "datageneration";
        public const string SUBJECT2 = "subject2";
        public const string BODY2 = "body2";


        private string xmlPath;
        private Dictionary dict = null;

        public XMLUtils(string xmlPath)
        {
            this.xmlPath = xmlPath;

            if (File.Exists(xmlPath)){
                readXml(xmlPath);
            } else { 
                createXml(xmlPath);
            }
        }

        private void createXml(string xmlPath)
        {
            XmlTextWriter writer = new XmlTextWriter(xmlPath, System.Text.Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 4;

            // 
            writer.WriteStartDocument(); 

            writer.WriteStartElement(INFO);

            writer.WriteStartElement(DATABASE);
            writer.WriteStartElement(CONTECTION);
            writer.WriteCData(dbContection);
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.WriteStartElement(SMTP); 
            writer.WriteElementString(HOST, "SMTP HOST");
            writer.WriteElementString(PORT, "SMTP PORT");
            writer.WriteElementString(USERNAME, "SMTP USERNAME");
            writer.WriteElementString(PASSWORD, "SMTP PASSWORD");
            writer.WriteEndElement();

            writer.WriteStartElement(MAIL); 
            writer.WriteElementString(FROM, "FROM");
            writer.WriteElementString(TO, "TO");

            writer.WriteStartElement(SENDMAIL); 
            writer.WriteElementString(SUBJECT1, "SUBJECT1");
            writer.WriteStartElement(BODY1);
            writer.WriteCData("BODY1");
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.WriteStartElement(DATAGENERATION);
            writer.WriteElementString(SUBJECT2, "SUBJECT2");
            writer.WriteStartElement(BODY2);
            writer.WriteCData("BODY2");
            writer.WriteEndElement();
            writer.WriteEndElement(); 

            writer.WriteEndElement(); 

            writer.WriteEndElement();

            writer.Close(); 
        }

        private void readXml(string xmlPath)
        {
            dict = new Dictionary();

            XmlTextReader reader = new XmlTextReader(new StreamReader(xmlPath, System.Text.Encoding.UTF8));
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.LocalName == CONTECTION)
                    {
                        dict.Add(CONTECTION, reader.ReadString().Trim());
                    }
                    if (reader.LocalName == HOST)
                    {
                        dict.Add(HOST, reader.ReadString().Trim());
                    }
                    if (reader.LocalName == PORT)
                    {
                        dict.Add(PORT, reader.ReadString().Trim());
                    }
                    if (reader.LocalName == USERNAME)
                    {
                        dict.Add(USERNAME, reader.ReadString().Trim());
                    }
                    if (reader.LocalName == PASSWORD)
                    {
                        dict.Add(PASSWORD, reader.ReadString().Trim());
                    }
                    if (reader.LocalName == FROM)
                    {
                        dict.Add(FROM, reader.ReadString().Trim());
                    }
                    if (reader.LocalName == TO)
                    {
                        dict.Add(TO, reader.ReadString().Trim());
                    }
                    if (reader.LocalName == SUBJECT1)
                    {
                        dict.Add(SUBJECT1, reader.ReadString().Trim());
                    }
                    if (reader.LocalName == BODY1)
                    {
                        dict.Add(BODY1, reader.ReadString().Trim());
                    }
                    if (reader.LocalName == SUBJECT2)
                    {
                        dict.Add(SUBJECT2, reader.ReadString().Trim());
                    }
                    if (reader.LocalName == BODY2)
                    {
                        dict.Add(BODY2, reader.ReadString().Trim());
                    }
                }
            }
        }

        public Dictionary getInfo()
        {
            return dict;
        }
    }
}

 

生成XML文件如下:

 

<?xml version="1.0" encoding="utf-8"?>
<info>
    <database>
        <contection><![CDATA[Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={HOST})(PORT={PORT})))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={SERVICE_NAME})));User Id={UID};Password={PWD};]]></contection>
    </database>
    <smtp>
        <host>SMTP POST</host>
        <port>SMTP PORT</port>
        <username>SMTP USERNAME</username>
        <password>SMTP PASSWORD</password>
    </smtp>
    <mail>
        <from>FROM</from>
        <to>TO</to>
        <sendmail>
            <subject1>SUBJECT1subject1>
            <body1><![CDATA[BODY1]]></body1>
        </sendmail>
        <datageneration>
            <subject2>SUBJECT2</subject2>
            <body2><![CDATA[BODY2]]></body2>
        </datageneration>
    </mail>
</info>
 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值