XML讀取信息並顯示

這個類命名叫Message.cs

namespace Common { public class Message { /// <summary> /// 信息編號 /// </summary> private string messagecode = string.Empty; public string MessageCode { get { return messagecode; } } /// <summary> /// 信息描述 /// </summary> private string messagedata = string.Empty; public string MessageData { get { return messagedata; } } public void Setmessagedata(string value) { messagedata = value; } public Message(string mc,int x) { this.messagedata = mc; } /// <summary> /// 構造函數 /// </summary> /// <param name="mc">信息編號</param> public Message(string mc) { this.messagecode = mc; using (System.Data.DataSet ds = XMLHelp.ConvertXMLFileToDataSet(System.Configuration.ConfigurationManager.AppSettings["MessagePath"])) { if (ds.Tables[0] != null) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if (ds.Tables[0].Rows[i]["Code"].ToString() == mc) { this.messagedata = ds.Tables[0].Rows[i]["Content"].ToString(); break; } } } } } } }

 下面這個類命名叫:XMLHelp.cs函數

都放在DAL文件裏面,本身修改命名空間this

using System;
using System.Text;
using System.Xml;
using System.Data;
using System.IO;
using System.Web;

namespace Common
{
    public class XMLHelp
    {/// <summary>
        /// 將xml文件轉換爲DataSet
        /// </summary>
        /// <param name="xmlFile">XML文件路徑</param>
        /// <returns>DataSet</returns>
        public static DataSet ConvertXMLFileToDataSet(string xmlFile)
        {
            StringReader sReader = null;
            XmlTextReader reader = null;
            try
            {
                XmlDocument xDoc = new XmlDocument();
                try
                {
                    xDoc.Load(xmlFile);
                }
                catch (DirectoryNotFoundException)
                {
                    xmlFile = Path.Combine(HttpContext.Current.Server.MapPath("~"), xmlFile);
                    xDoc.Load(xmlFile);
                }
                DataSet dResult = new DataSet();
                sReader = new StringReader(xDoc.InnerXml);
                reader = new XmlTextReader(sReader);
                dResult.ReadXml(reader);
                return dResult;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (reader != null) reader.Close();
            }
        }

        /// <summary>
        /// 將DataSet轉換爲xml文件
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="xmlFile"></param>
        public static void ConvertDataSetToXMLFile(DataSet ds, string xmlFile)
        {
            MemoryStream stream = null;
            XmlTextWriter writer = null;

            try
            {
                stream = new MemoryStream();
                //從stream裝載到XmlTextReader
                writer = new XmlTextWriter(stream, Encoding.Unicode);

                //用WriteXml方法寫入文件.
                ds.WriteXml(writer);
                int count = (int)stream.Length;
                byte[] arr = new byte[count];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(arr, 0, count);

                //返回Unicode編碼的文本
                UnicodeEncoding utf = new UnicodeEncoding();
                StreamWriter sw = new StreamWriter(xmlFile);
                sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                sw.WriteLine(utf.GetString(arr).Trim());
                sw.Close();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (writer != null) writer.Close();
            }
        }
    }
}

 

 

如:後臺彈窗:編碼

                    string ID = Request["ID"];
                    if (!Function.CheckStr(ID, 1))
                    {
                        Function.ShowMsgBox(new Message("M0008").MessageData, "baoming.aspx", false);  //掉出方法,而後給定參數,直接執行就OK
                    }spa

在項目根目錄有個XML文件夾,裏面有個Message.xml文件,內容以下:code

這個XML文件裏的Code本身定義,content就是你要讀取的內容,須要的時候匹配好Code字段就行了。就是這麼簡單xml

相關文章
相關標籤/搜索