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>
 

 

 

 

 

你可能感兴趣的:(C#)