C# Ini、Json、Xml 封装类
/// <summary>
/// INI文件读取类
/// </summary>
public class INIHelper
{
/// <summary>
/// 文件名
/// </summary>
public static string str = "setting.ini";
/// <summary>
/// 文件路径
/// </summary>
public static string path = System.AppDomain.CurrentDomain.BaseDirectory + str; [DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); [DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath); /// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public static void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, path);
} /// <summary>
/// 读取INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">键</param>
/// <param name="path">文件路径</param>
/// <returns></returns>
public static string IniReadValue(string Section, string Key, string path)
{
StringBuilder temp = new StringBuilder();
int i = GetPrivateProfileString(Section, Key, "", temp, , path);
return temp.ToString();
} /// <summary>
/// 读取INI文件
/// </summary>
/// <param name="section">节点名称</param>
/// <param name="key">键</param>
/// <returns></returns>
public static byte[] IniReadValues(string section, string key)
{
byte[] temp = new byte[];
int i = GetPrivateProfileString(section, key, "", temp, , path);
return temp; }
/// <summary>
/// 删除ini文件下所有节点
/// </summary>
public static void ClearAllSection()
{
IniWriteValue(null, null, null);
}
/// <summary>
/// 删除ini文件指点节点下的所有键
/// </summary>
/// <param name="Section"></param>
public static void ClearSection(string Section)
{
IniWriteValue(Section, null, null);
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static T DeserializeObject<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
/// <summary>
/// 序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static string DeserializeObject<T>(object obj)
{
JsonConvert.SerializeObject(obj);
}
public class AppConfigXmlHelper
{
public static string AppConfig()
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
string strDirectoryPath = Environment.CurrentDirectory + "\\IniJsonXmlTest.EXE.config";
return strDirectoryPath;
}
public static string GetPath(string key)
{
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(AppConfig());
XmlNode xNode;
XmlElement xElem;
xNode = xDoc.SelectSingleNode("//appSettings"); //补充,需要在你的app.config 文件中增加一下,<appSetting> </appSetting>
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
catch (Exception)
{
return "";
}
}
public static void SetPath(string key, string path)
{
XmlElement xElem;
XmlDocument doc = new XmlDocument();
doc.Load(AppConfig());
XmlNode node = doc.SelectSingleNode(@"//appSettings");
xElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
xElem.SetAttribute("value", path);
doc.Save(AppConfig());
}
}
public class XmlSerialization
{ public static System.IO.Stream Serialize(object obj, Encoding encode, System.IO.Stream s)
{
TextWriter writer = null;
writer = new StreamWriter(s, encode); XmlSerializer ser = new XmlSerializer(obj.GetType()); ser.Serialize(writer, obj);
return s; } public static MemoryStream Serialize(object obj, Encoding encode)
{
MemoryStream s = new MemoryStream();
Serialize(obj, encode, s);
s.Position = ;
return s;
} public static MemoryStream Serialize(object obj, String encode)
{
return Serialize(obj, Encoding.GetEncoding(encode));
} public static MemoryStream Serialize(object obj)
{
return Serialize(obj, Encoding.UTF8);
} public static object Deserialize(System.IO.Stream In, Type objType)
{
In.Position = ;
XmlSerializer ser = new XmlSerializer(objType);
return ser.Deserialize(In);
}
} public class XmlSerialization<T>
{
public static System.IO.Stream Serialize(T obj, Encoding encode, System.IO.Stream s)
{
TextWriter writer = null;
writer = new StreamWriter(s, encode); XmlSerializer ser = new XmlSerializer(typeof(T)); ser.Serialize(writer, obj);
return s;
} public static MemoryStream Serialize(T obj, Encoding encode)
{
MemoryStream s = new MemoryStream();
Serialize(obj, encode, s);
s.Position = ;
return s;
} public static MemoryStream Serialize(T obj, String encode)
{
return Serialize(obj, Encoding.GetEncoding(encode));
} public static MemoryStream Serialize(T obj)
{
return Serialize(obj, Encoding.UTF8);
} public static T Deserialize(System.IO.Stream In)
{
In.Position = ;
XmlSerializer ser = new XmlSerializer(typeof(T));
return (T)ser.Deserialize(In);
}
}
- 序列化: 将数据结构或对象转换成二进制串的过程。
- 反序列化:将在序列化过程中所生成的二进制串转换成数据结构或者对象的过程。
public static class Serializer
{ #region 二进制
/// <summary>
/// 序列化二进制文件
/// </summary>
/// <param name="path"></param>
/// <param name="data"></param>
public static void SaveBinary(string path, object data)
{
if (File.Exists(path))
{
File.Delete(path);
}
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = File.Create(path);
bf.Serialize(fs, data);
fs.Close();
} /// <summary>
/// 反序列化二进制文件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path"></param>
/// <returns></returns>
public static T LoadBinary<T>(string path)
{
if (!File.Exists(path))
{
return default(T);
}
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = File.OpenRead(path);
object data = bf.Deserialize(fs);
fs.Close();
return (T)data;
}
#endregion #region xml
/// <summary>
/// 序列化XML文件
/// </summary>
/// <param name="path"></param>
/// <param name="data"></param>
/// <param name="type"></param>
public static void SaveXml(string path, object data, Type type)
{
if (File.Exists(path))
{
File.Delete(path);
}
XmlSerializer xs = new XmlSerializer(type);
FileStream fs = File.Create(path);
xs.Serialize(fs, data);
fs.Close();
} /// <summary>
/// 反序列化XML文件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path"></param>
/// <param name="type"></param>
/// <returns></returns>
public static T LoadXml<T>(string path, Type type)
{
if (!File.Exists(path))
{
return default(T);
}
XmlSerializer xs = new XmlSerializer(type);
FileStream fs = File.OpenRead(path);
object data = xs.Deserialize(fs);
fs.Close();
return (T)data;
}
#endregion #region json
/// <summary>
/// 序列化JSON
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static string Serialize(object data)
{
JavaScriptSerializer json = new JavaScriptSerializer();
json.MaxJsonLength = Int32.MaxValue;
return json.Serialize(data);
} /// <summary>
/// 反序列化JSON
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static T Deserialize<T>(string json)
{
JavaScriptSerializer js = new JavaScriptSerializer();
js.MaxJsonLength = Int32.MaxValue;
return js.Deserialize<T>(json);
}
#endregion }
C# Ini、Json、Xml 封装类的更多相关文章
- c#通用配置文件读写类(xml,ini,json)
.NET下编写程序的时候经常会使用到配置文件.配置文件格式通常有xml.ini.json等几种,操作不同类型配置文件需要使用不同的方法,操作较为麻烦.特别是针对同时应用不同格式配置文件的时候,很容易引 ...
- c#通用配置文件读写类与格式转换(xml,ini,json)
.NET下编写程序的时候经常会使用到配置文件.配置文件格式通常有xml.ini.json等几种,操作不同类型配置文件需要使用不同的方法,操作较为麻烦.特别是针对同时应用不同格式配置文件的时候,很容易引 ...
- Python导出Excel为Lua/Json/Xml实例教程(三):终极需求
相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 Python导出E ...
- Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验
Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...
- Python导出Excel为Lua/Json/Xml实例教程(一):初识Python
Python导出Excel为Lua/Json/Xml实例教程(一):初识Python 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出 ...
- JSON&XML总结
JSON&XML: JSON----- //英译 Serialization:序列化 perform:执行 segue:继续 IOS5后 NSJSONSerialization解析 解析JSO ...
- JSON/XML序列化与反序列化(非构造自定义类)
隔了很长时间再重看自己的代码,觉得好陌生..以后要养成多注释的好习惯..直接贴代码..对不起( ▼-▼ ) 保存保存:进行序列化后存入应用设置里 ApplicationDataContainer _a ...
- php返回json,xml,JSONP等格式的数据
php返回json,xml,JSONP等格式的数据 返回json数据: header('Content-Type:application/json; charset=utf-8'); $arr = a ...
- Atitit.json xml 序列化循环引用解决方案json
Atitit.json xml 序列化循环引用解决方案json 1. 循环引用1 2. 序列化循环引用解决方法1 2.1. 自定义序列化器1 2.2. 排除策略1 2.3. 设置序列化层次,一般3级别 ...
- 【转】[WCF REST] 帮助页面与自动消息格式(JSON/XML)选择
可以说WebHttpBinding和WebHttpBehavior是整个Web HTTP编程模型最为核心的两个类型,前者主要解决消息编码问题,而余下的工作基本上落在了终结点行为WebHttpBehav ...
随机推荐
- jquery 判断浏览器版本
如果你也是Jquery最初的使用者,那么你一定经历过这样判断浏览器的时代:$.browser.msie && $.browser.version,你目前使用的组件里可能还有应用.但是J ...
- Could not get JDBC Connection--java
postMan上调用合同服务,后台运行错误,如下: { "timestamp": 1536203887641, "status": 500, "err ...
- python数据结构与算法之算法和算法分析
1.问题.问题实例.算法的概念区分. 一个例子说明一下: 问题:判断一个正整数N是否为素数 #问题是需要解决的一个需求 问题实例:判断1314是否为素数? #问题实例是该问题的一个具体例子 算法: ...
- 初识HT for web
目前国内经济转型在潜移默化中已经发生了巨大的变化,保险,零售业,汽车等我能想到的. 只要互联网能插足的行业,都难逃一‘劫’. 刚看了一篇博客--基于 HTML5 的工业组态高炉炼铁 3D 大屏可视化 ...
- Vue中的钩子
每个Vue实例被创建后都要经历初始化过程.在这个过程中也会运行一些叫做生命周期钩子的函数,方便用户在不同阶段进行不同的代码实现. 1.Created 在实例创建完成后立即执行的函数. <!DOC ...
- Python2.x爬虫入门之URLError异常处理
大家好,本节在这里主要说的是URLError还有HTTPError,以及对它们的一些处理. 1.URLError 首先解释下URLError可能产生的原因: (1)网络无连接,即本机无法上网 (2)连 ...
- spring websocket报错:No matching message handler methods.
错误信息: [org.springframework.web.socket.messaging.WebSocketAnnotationMethodMessageHandler]-[DEBUG] No ...
- lvs,nginx反向代理,虚拟主机
LVS NAT 拓扑 client | | LVS | | ------------------- | | | RS1 RS2 RS3 地址规划如下 机器名称 ip配置 ip配置 备注信息 LVS 1 ...
- [Oracle][DATAGUARD] PHYSICAL STANDBY环境里,11.2.0.4 , 也可以使用Pfile来运行Primary和Standby(虽然很少有人用)
####Primary#### [oracle@primary ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.4.0 Production on 金 ...
- SQL-57 使用含有关键字exists查找未分配具体部门的员工的所有信息。
题目描述 使用含有关键字exists查找未分配具体部门的员工的所有信息.CREATE TABLE `employees` (`emp_no` int(11) NOT NULL,`birth_date` ...