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都没有了的话,连启动都不行的话,有什么方法恢复它呢?答案是方法不只一种,而是两种.其实是我 ...
随机推荐
- PAT Basic 1009 说反话 (20 分)
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小 ...
- usb server
usb server 是通道型的,驱动在客户端安装 服务端不需要驱动
- 双向链表/list
双向链表结构如下 双向链表结构中元素在内存中不是紧邻空间,而是每个元素中存放上一个元素和后一个元素的地址 第一个元素称为头(head)元素,前连接(前置指针域)为nil 最后一个元素称为尾(foot) ...
- 更换Red Hat Enterprise Linux 7 64位的yum为centos的版本
查看redhat原有的yum包有哪些: [root@localhost ~]# rpm -qa|grep yum yum-utils-1.1.31-24.el7.noarch yum-langpack ...
- mybatis 分页插件PageHelper的简单使用
首先在pom.xml配置文件中增加相关的插件. 插件地址:https://github.com/pagehelper/Mybatis-PageHelper <dependency> < ...
- vue-router嵌套路由,二级路由。
如果全是用一级路由时,路由管理就变得很臃肿,有点乱,路由有父子关系的话,嵌套路由会更好.嵌套也就是路由中的路由的意思,组件中可以有自己的路由导航和路由容器(router-link.router-vie ...
- META标签的设置
㈠定义及用法 ⑴<meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. ⑵<meta> 标签位于文档的头部,不 ...
- luoguP1034 矩形覆盖 x
P1034 矩形覆盖 题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4( ...
- JavaWeb_(设计模式)单例模式
菜鸟教程 传送门 单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 这种模式涉及到一个单一的类,该 ...
- linux下安装R
下载 版本:R3.2.2 下载地址:http://cran.rstudio.com/src/base/R-3/R-3.2.2.tar.gz 安装步骤: 解压 tar -zxvf R-3.2.2.tar ...