Custom Configuration 的两种方法:2.XmlSerializer XmlAttribute
第二种:XmlSerializer XmlAttribute
2.CustomConfigurationSetting.cs
3.CustomConfigurationManger.cs
1.CustomConfiguration.xml
<?xml version="1.0" encoding="utf-8" ?>
<CustomSettings>
<SElement name="name01" value="value01" />
<M2Element>
<SElement name="name02" value="value02" />
<SElement name="name03" value="value03" />
</M2Element>
</CustomSettings>
2.CustomConfigurationSetting.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;
using System.Collections.Specialized;
using System.Collections; namespace CustomConfigurationSection
{
[XmlRoot("CustomSettings")]
public class CustomConfigurationSettings
{
[XmlElement("SElement")]
public SElementClazz SElement; private ArrayList _sElementList = new ArrayList(); [XmlArrayItem("SElement")]
[XmlArray("M2Element")]
public SElementClazz[] SElementCollection
{
get
{
SElementClazz[] items = new SElementClazz[_sElementList.Count];
_sElementList.CopyTo(items);
return items;
}
set {
SElementClazz[] items = (SElementClazz[])value;
_sElementList.Clear();
foreach (SElementClazz i in items)
_sElementList.Add(i);
}
} } public class SElementClazz
{
private string _name;
private string _value; [XmlAttribute("name")]
public string Name
{
get { return this._name; }
set { this._name = value; }
} [XmlAttribute("value")]
public string Value
{
get { return this._value; }
set { this._value = value; }
}
}
}
3.CustomConfigurationManager.cs
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization; namespace CustomConfigurationSection
{
public class CustomConfigurationManager
{
public static T Create<T>(string config, string path)
{
if (!File.Exists(config))
{
return default(T);
}
XmlDocument doc = new XmlDocument();
doc.Load(config);
XmlNode section = doc.SelectSingleNode(path);
return Create<T>(null, null, section);
} public static T Create<T>(object parent, object configContext, System.Xml.XmlNode section)
{
T settings = default(T);
if (section == null) { return settings; }
XmlSerializer xs = new XmlSerializer(typeof(T));
XmlNodeReader reader = new XmlNodeReader(section);
settings = (T)xs.Deserialize(reader);
return settings;
}
}
}
4.TestXmlSerializer.cs
public void TestXmlserializer()
{
try
{
CustomConfigurationSettings config = CustomConfigurationManager.Create<CustomConfigurationSettings>("CustomConfiguration.xml", "/CustomSettings");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
http://blog.csdn.net/zpx3243/article/details/5978604
Custom Configuration 的两种方法:2.XmlSerializer XmlAttribute的更多相关文章
- Custom Configuration 的两种方法:1.Configuration Sections
第一种Configuration Sections 1.App.config 2.CustomConfigurationManager.cs 3.TestProgram.cs. App.config ...
- C# web api返回类型设置为json的两种方法
web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...
- C# web api 返回类型设置为json的两种方法
每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...
- Linux安装MySQL的两种方法
转载:http://blog.csdn.net/superchanon/article/details/8546254/ 1. 运行平台:CentOS 6.3 x86_64,基本等同于RH ...
- hive权威安装出现的不解错误!(完美解决)两种方法都可以
以下两种方法都可以,推荐用方法一! 方法一: 步骤一: yum -y install mysql-server 步骤二:service mysqld start 步骤三:mysql -u root - ...
- 读取xml文件转成List<T>对象的两种方法(附源码)
读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法, ...
- 取xml文件转成List<T>对象的两种方法
读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最 ...
- web项目docker化的两种方法
标题所讲的两种方法其实就是创建docker镜像的两种方法 第一种:启动镜像后进入容器中操作,将需要的软件或者项目移动到容器中,安装或者部署,然后退出即可 第二种:编写dockerfile,将需要的镜像 ...
- Cisco设备IOS的恢复方法 两种方法
如果不小心把Router或者Switch的IOS删除了,特别是Flash中的IOS和ROM中的Mini IOS都没有了的话,连启动都不行的话,有什么方法恢复它呢?答案是方法不只一种,而是两种.其实是我 ...
随机推荐
- python-嵌套函数
python-嵌套函数 定义:在函数体内用def定义一个函数,它的作用域只在该函数体内有效. def outside(): print("int the outside") def ...
- Lua语言基本语法~运算符
Lua 变量 变量在使用前,必须在代码中进行声明,即创建该变量. 编译程序执行代码之前编译器需要知道如何给语句变量开辟存储区,用于存储变量的值. Lua 变量有三种类型:全局变量.局部变量.表中的域. ...
- java8学习之groupingBy源码分析
继续接着上一次[http://www.cnblogs.com/webor2006/p/8366083.html]来分析Collectors中的各种收集器的实现, 对里它里面有个groupingby() ...
- redis事务(转载)
原文地址:http://blog.csdn.net/hechurui/article/details/49508749 Redis事务 首先,Redis本身是单线程的. redis中的事务(trans ...
- DB2中常见sqlCode原因分析
000 | 00000 | SQL语句成功完成 01xxx | SQL语句成功完成,但是有警告 +012 | 01545 | 未限定的列名被解释为一个有相互关系的引用 +098 | 01568 | 动 ...
- java读取word文档,提取标题和内容
使用的工具为poi,需要导入的依赖如下 <dependency> <groupId>org.apache.poi</groupId> <artifactId& ...
- Newsgroups数据集研究
1.数据集介绍 20newsgroups数据集是用于文本分类.文本挖据和信息检索研究的国际标准数据集之一. 数据集收集了大约20,000左右的新闻组文档,均匀分为20个不同主题的新闻组集合. 一些新闻 ...
- qt5-Qt Creator使用
设置编码: 工具-->选项-->文本编辑器-->行为-->编辑器 中文编译失败的解决: 编辑-->--> 在头文件中增加:--解决乱码问题(文本所在的头文件) #i ...
- wx小程序知识点(七)
七.小程序提速与性能优化 参考大佬vicyao的文章 https://blog.csdn.net/wetest_tencent/article/details/61196522 (1)提高页面加载速度 ...
- 闰年计算——JavaScript 语言计算
㈠闰年是如何来的? 闰年(Leap Year)是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的.补上时间差的年份为闰年. ㈡什么是闰年? 凡阳历中有闰日(二月为二十九日)的年, ...