zoukankan      html  css  js  c++  java
  • 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文件里面,自己修改命名空间

    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
                        }

    在项目根目录有个XML文件夹,里面有个Message.xml文件,内容如下:

    这个XML文件里的Code自己定义,content就是你要读取的内容,需要的时候匹配好Code字段就好了。就是这么简单

  • 相关阅读:
    ASP.NET中Session,Application,Viewstate,Cache,隐藏域和带参数的传接收值的用法
    JS页面跳转搜集
    SQL中常用的日期转化
    DIV+CSS兼容性解决IE6/IE7/FF浏览器的通用方法
    C#中将数据导出为EXCEL方式汇总
    正则表达式大全
    ASP.NET中Cookie用法小节
    div+CSS浏览器兼容问题整理
    站长常用的200个js代码
    [转]主机和终端
  • 原文地址:https://www.cnblogs.com/Shd-Study/p/4468654.html
Copyright © 2011-2022 走看看