• XMLHelper 类


    XMLHelper 类

    View Code

      1  public class XmlHelper
    2 {
    3 public XmlHelper()
    4 {
    5 //构造函数
    6 }
    7
    8 #region 生成XML文件
    9 ///<summary>
    10 /// 生成XML文件
    11 ///</summary>
    12 ///<param name="Ename"></param>
    13 public static void saveClassXML(string Ename)
    14 {
    15 StreamWriter sw = null;
    16 string FileName = HttpContext.Current.Server.MapPath("~/xml/Content/" + Ename + ".xml");
    17 if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/xml/Content")))
    18 {
    19 Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/xml/Content"));
    20 }
    21 sw = File.CreateText(FileName);
    22 sw.WriteLine("<?xml version=\"1.0\" encoding=\"gb2312\"?>\r");
    23 sw.WriteLine("<rss version=\"2.0\">\r");
    24 sw.WriteLine("<channel>\r");
    25 sw.WriteLine("<item>\r");
    26 sw.WriteLine("</item>\r");
    27 sw.WriteLine("</channel>\r");
    28 sw.WriteLine("</rss>\r");
    29 sw.Flush();
    30 sw.Close();
    31 sw.Dispose();
    32 }
    33 #endregion
    34
    35 #region 保存配置文件
    36 ///<summary>
    37 /// 保存配置
    38 ///</summary>
    39 ///<param name="strTarget">接点名</param>
    40 ///<returns></returns>
    41 public static void SaveXmlConfig(string strTarget, string strValue, string strSource)
    42 {
    43 string xmlPath = HttpContext.Current.Server.MapPath("~/" + strSource);
    44 System.Xml.XmlDocument xdoc = new XmlDocument();
    45 xdoc.Load(xmlPath);
    46 XmlElement root = xdoc.DocumentElement;
    47 XmlNodeList elemList = root.GetElementsByTagName(strTarget);
    48 elemList[0].InnerXml = strValue;
    49 xdoc.Save(xmlPath);
    50 }
    51 #endregion
    52
    53 #region 读取全部配置文件
    54 ///<summary>
    55 /// 读取配置
    56 ///</summary>
    57 ///<param name="strTarget">接点名</param>
    58 ///<returns></returns>
    59 public static string readparamConfig(string strTarget)
    60 {
    61 string rstr = "";
    62 string xmlPath = HttpContext.Current.Server.MapPath("~/xml/base.config");
    63 FileInfo finfo = new FileInfo(xmlPath);
    64 System.Xml.XmlDocument xdoc = new XmlDocument();
    65 xdoc.Load(xmlPath);
    66 XmlElement root = xdoc.DocumentElement;
    67 XmlNodeList elemList = root.GetElementsByTagName(strTarget);
    68 rstr += elemList[0].InnerXml;
    69 return rstr;
    70 }
    71 #endregion
    72
    73 #region 读取配置文件
    74 ///<summary>
    75 /// 读取配置
    76 ///</summary>
    77 ///<param name="strTarget">接点名</param>
    78 ///<param name="type"></param>
    79 ///<returns></returns>
    80 public static string readparamConfig(string strTarget, string type)
    81 {
    82 string rstr = "";
    83 if (type != null && type != string.Empty)
    84 {
    85 string xmlPath = HttpContext.Current.Server.MapPath("~/xml/" + type + ".config");
    86 FileInfo finfo = new FileInfo(xmlPath);
    87 System.Xml.XmlDocument xdoc = new XmlDocument();
    88 xdoc.Load(xmlPath);
    89 XmlElement root = xdoc.DocumentElement;
    90 XmlNodeList elemList = root.GetElementsByTagName(strTarget);
    91 rstr += elemList[0].InnerXml;
    92 }
    93 else
    94 {
    95 rstr = readparamConfig(strTarget);
    96 }
    97 return rstr;
    98 }
    99 #endregion
    100
    101 #region 读取xml文件节点
    102 ///<summary>
    103 /// 读取配置
    104 ///</summary>
    105 ///<param name="strTarget">接点名</param>
    106 ///<param name="type"></param>
    107 ///<returns></returns>
    108 public static string readparamXml(string strTarget, string path)
    109 {
    110 string rstr = "";
    111 if (path != null && path != string.Empty)
    112 {
    113 string xmlPath = HttpContext.Current.Server.MapPath(path);
    114 FileInfo finfo = new FileInfo(xmlPath);
    115 System.Xml.XmlDocument xdoc = new XmlDocument();
    116 xdoc.Load(xmlPath);
    117 XmlElement root = xdoc.DocumentElement;
    118 XmlNodeList elemList = root.GetElementsByTagName(strTarget);
    119 rstr += elemList[0].InnerXml;
    120 }
    121 return rstr;
    122 }
    123 #endregion
    124
    125 #region 读取XML文件返回一个DataTable
    126 ///<summary>
    127 /// 读取XML文件返回一个DataTable
    128 ///</summary>
    129 ///<param name="xml">XML文件路径</param>
    130 ///<returns>DataView对像</returns>
    131 public static DataTable GetXmlList(string xml)
    132 {
    133 string url = HttpContext.Current.Server.MapPath(xml);//获得当前文件夹下的XML文件
    134 StreamReader sRead = new StreamReader(url, System.Text.Encoding.GetEncoding("GB2312"));//以一种特定的编码从字节流读取字符,必须要转化成GB2312读取才不能出乱码
    135 XmlDataDocument datadoc = new XmlDataDocument();//操作XML文档
    136 datadoc.DataSet.ReadXml(sRead);//将读取的字节流存到DataSet里面去
    137 DataTable dv = datadoc.DataSet.Tables[0];
    138 datadoc = null;//清空对XML数据的操作
    139 sRead.Close();//关闭字节流的读取
    140 return dv;
    141 }
    142 #endregion
    143 }
  • 相关阅读:
    POJ 2195 Going Home (费用流)
    POJ 1087 A Plug for UNIX (网络流,最大流)
    凸包的直径——旋转卡壳
    凸包--Graham扫描法
    POJ 3167 Layout(差分约束)
    POJ 2187 Beauty Contest(凸包,旋转卡壳)
    HDU 1392 Surround the Trees(凸包)
    HDU 3416 Marriage Match IV(最短路,网络流)
    【USACO4.2】草地排水Drainage Ditches(最大流)
    【模板】网络最大流
  • 原文地址:https://www.cnblogs.com/cnscpz/p/2209955.html
Copyright © 2020-2023  润新知