read txt

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Collections;

namespace tixing
{
    public partial class Form1 : Form
    {
        public string xmlFilePath=".\\tixing.txt";//要删
        public static bool addBlank = false;//每次程序启动的时候要添加一条空数据
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ShowTixing();         
        }

        public void ShowTixing()
        {
            #region 替换中文等字符
            StreamReader srReplace = new StreamReader(xmlFilePath, Encoding.GetEncoding("GB2312"));
            string strLineReplace = "";
            string strAll = "";
            while (strLineReplace != null)
            {
                strLineReplace = srReplace.ReadLine();
                if (strLineReplace != null)
                {
                    strLineReplace = strLineReplace.Replace("/", "/").Replace(" ", " ").Replace("am", "AM").Replace("pm", "PM").Trim();

                    if (strLineReplace != "")
                    {
                        strAll += strLineReplace + "\r\n";
                    }
                }
            }
            //每天新加两行
            if (!addBlank)
            {
                strAll += "\r\n<" + DateTime.Now.ToString("MM-dd") + ">\r\n";
                if (DateTime.Now.Hour > 12)
                {
                    strAll += "PM";
                }
                else
                {
                    strAll += "AM";
                }
                strAll += DateTime.Now.ToString("hh:mm") + "";
                addBlank = true;
            }


            srReplace.Close();

            FileStream fileStream = new FileStream(".\\tixing.txt", FileMode.Truncate);
            StreamWriter sw = new StreamWriter(fileStream, Encoding.GetEncoding("GB2312"));
            sw.Write(strAll);
            sw.Flush();
            sw.Close();//下面还要用一次,所以不关闭了
            fileStream.Close();
            #endregion


            #region 提取出XML的字符串
            StreamReader sr = new StreamReader(xmlFilePath, Encoding.GetEncoding("GB2312"));

            string strLine = "";
            string strLineTrim = "";
            string strXML = "<?xml version=\"1.0\" encoding=\"gb2312\"?><tixing>";
            string strTempDate = "";
            while (strLine != null)
            {
                strLine = sr.ReadLine();
                if (strLine != null)
                {
                    strLineTrim = strLine.Trim();
                    //日期行
                    if (strLineTrim != "" && strLine.Substring(0, 1) == "<" && strLine.Substring(strLine.Length - 1, 1) == ">")
                    {
                        strTempDate = ReturnDate(strLineTrim) + " ";

                    }
                    //时间行
                    else if (strLineTrim != "")
                    {
                        string strShowTemp = "";
                        if (strLineTrim.Substring(0, 2) != "//")
                        {
                            strShowTemp = "yes";
                        }
                        else
                        {
                            strLineTrim = strLineTrim.Replace("//", "");
                            strShowTemp = "no";
                        }
                        string[] arrLine = strLineTrim.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        string arrLine1;//用来充当数组的第二个元素,这是考虑只有时间没有提示内容的情况
                        if (arrLine.Length < 2)
                            arrLine1 = "";
                        else
                            arrLine1 = arrLine[1];
                        if (isInt(arrLine[0]))
                            strXML += "<item t=\"" + ToLongTimeInt(arrLine[0]) + "\" show=\"" + strShowTemp + "\">" + arrLine1 + "</item>";
                        else
                            strXML += "<item t=\"" + ToLongTime(strTempDate + ReturnTime(arrLine[0])) + "\" show=\"" + strShowTemp + "\">" + arrLine1 + "</item>";
                    }
                    else
                    {
                    }
                }
                else
                {
                    strXML += "</tixing>";
                }
            }
            sr.Close();
            //textBox1.Text = strXML;
            #endregion


            #region 读取xml字符串,并判断提醒
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(strXML);
            XmlNodeList nodeList;
            nodeList = xmlDoc.SelectNodes("/tixing/item[@show='yes']");
            string strTxt = "";
            foreach (XmlNode xn in nodeList)
            {
                XmlElement xe = (XmlElement)xn;
                strTxt = Convert.ToString(xe.InnerText).Trim();
                DateTime txTime = DateTime.Now;
                try
                {
                    txTime = Convert.ToDateTime(xe.GetAttribute("t"));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("时间为:" + xe.GetAttribute("t") + "\n\n错误为:" + ex.ToString());
                    return;
                }
                if (txTime < DateTime.Now && strTxt !="")
                {
                    Show();
                    lblTixing.Text = strTxt;
                    xe.SetAttribute("show", "no");
                    break;
                }
            }
            #endregion

            #region 写入txt文件
            string strTime = "";
            string strShow = "";//是否显示过,如show="yes"
            string strDateInTime = "";//从时间串中取出日期串
            string strDateInTimeTemp = "";//临时时间
            nodeList = xmlDoc.SelectNodes("/tixing/item");
            strAll = "";//清空前面的strAll

            foreach (XmlNode xn in nodeList)
            {
                XmlElement xe = (XmlElement)xn;
                strTime = xe.GetAttribute("t");
                strDateInTime = strTime.Substring(5, 5);
                strTxt = Convert.ToString(xe.InnerText).Trim();
                strShow = Convert.ToString(xe.GetAttribute("show")).Trim();
                if (strDateInTimeTemp != strDateInTime)
                {
                    strDateInTimeTemp = strDateInTime;
                    strAll += "<" + strDateInTime + ">\r\n";
                }
                if(strShow=="yes")
                    strAll += AddAMPM(strTime.Substring(11,5)) + " " + strTxt + "\r\n";
                else
                    strAll += "//" + AddAMPM(strTime.Substring(11, 5)) + " " + strTxt + "\r\n";
            }
            fileStream = new FileStream(".\\tixing.txt", FileMode.Truncate);
            sw = new StreamWriter(fileStream, Encoding.GetEncoding("GB2312"));
            sw.Write(strAll);
            sw.Flush();
            sw.Close();
            fileStream.Close();
            #endregion

            SetPosition();
        }

        public void SetPosition()
        {
            lblTixing.Left = this.Width / 2 - lblTixing.Width/2;
            btnHide.Left = this.Width / 2 - btnHide.Width/2;
            btnHide.Focus();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
           ShowTixing();
        }

        public string AddAMPM(string inputTime)
        {
            string strMinute = inputTime.Substring(0, 2);
            int intTime = Convert.ToInt32(strMinute);
            if (intTime > 12)
            {
                intTime = intTime - 12;
                return "PM" + intTime.ToString()+ inputTime.Substring(2, 3);
            }
            else
            {
                return "AM" + inputTime;
            }

        }


        private void btnHide_Click(object sender, EventArgs e)
        {
            Hide();
        }


        public string ToLongTime(string shorttime)
        {
            return DateTime.Now.Year.ToString()+ "-" + shorttime + ":00";
        }

        public string ToLongTimeInt(string strMinute)
        {
            return DateTime.Now.AddMinutes(Convert.ToDouble(strMinute)).ToString("yyyy-MM-dd HH:mm:ss");
        }


        /// <summary>
        /// 判断一个字串是否为Int型
        /// </summary>
        public bool isInt(string str)
        {
            try
            {
                int num = int.Parse(str);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 分钟的数字->时间+数字
        /// </summary>
        /// <param name="inputMinute"></param>
        /// <returns></returns>
        public string MinuteToTime(string inputMinute)
        {
            string minuteStr="";
            DateTime dt=DateTime.Now.AddMinutes(Convert.ToDouble(inputMinute));
            if (dt.Hour < 12)
                minuteStr = "AM"+dt.ToString("hh:mm");
            else
            {
                minuteStr = "PM" + dt.ToString("hh:mm");
            }
            return minuteStr;
        }

        public string ReturnDate(string str)
        {
            string[] arr = str.Split(new Char[] { '<', '-', '>' }, StringSplitOptions.RemoveEmptyEntries);
            if (arr[0].Length == 1)
                arr[0] = "0" + arr[0];
            if (arr[1].Length == 1)
                arr[1] = "0" + arr[1];
            return arr[0] + "-" + arr[1];
        }
        public string ReturnTime(string str)
        {
            string strLeftTwo=str.Substring(0,2);
            string[] arr = str.Replace("AM","").Replace("PM","").Split(new Char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            if (strLeftTwo == "AM")
            {
                if (arr[0].Length == 1)
                    arr[0] = "0" + arr[0];
                if (arr[1].Length == 1)
                    arr[1] = "0" + arr[1];
            }
            else if (strLeftTwo == "PM")
            {
                arr[0] = Convert.ToString(Convert.ToInt32(arr[0]) + 12);
                if (arr[1].Length == 1)
                    arr[1] = "0" + arr[1];
            }
            return arr[0] + ":" + arr[1];
        }

    }
}

你可能感兴趣的:(xml,windows)