第二种:XmlSerializer XmlAttribute

1.CustomConfiguration.xml

2.CustomConfigurationSetting.cs

3.CustomConfigurationManger.cs

4.TestXmlSerializer

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的更多相关文章

  1. Custom Configuration 的两种方法:1.Configuration Sections

    第一种Configuration Sections 1.App.config 2.CustomConfigurationManager.cs 3.TestProgram.cs. App.config ...

  2. C# web api返回类型设置为json的两种方法

    web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...

  3. C# web api 返回类型设置为json的两种方法

    每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...

  4. Linux安装MySQL的两种方法

    转载:http://blog.csdn.net/superchanon/article/details/8546254/ 1.       运行平台:CentOS 6.3 x86_64,基本等同于RH ...

  5. hive权威安装出现的不解错误!(完美解决)两种方法都可以

    以下两种方法都可以,推荐用方法一! 方法一: 步骤一: yum -y install mysql-server 步骤二:service mysqld start 步骤三:mysql -u root - ...

  6. 读取xml文件转成List<T>对象的两种方法(附源码)

    读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法, ...

  7. 取xml文件转成List<T>对象的两种方法

    读取xml文件转成List<T>对象的两种方法(附源码)   读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最 ...

  8. web项目docker化的两种方法

    标题所讲的两种方法其实就是创建docker镜像的两种方法 第一种:启动镜像后进入容器中操作,将需要的软件或者项目移动到容器中,安装或者部署,然后退出即可 第二种:编写dockerfile,将需要的镜像 ...

  9. Cisco设备IOS的恢复方法 两种方法

    如果不小心把Router或者Switch的IOS删除了,特别是Flash中的IOS和ROM中的Mini IOS都没有了的话,连启动都不行的话,有什么方法恢复它呢?答案是方法不只一种,而是两种.其实是我 ...

随机推荐

  1. Linux驱动开发之LED驱动

    首先讲下字符设备控制技术 : 大部分驱动程序除了需要提供读写设备的能力外,还需要具备控制设备的能力.比如: 改变波特率. 在用户空间,使用ioctl系统调用来控制设备,原型如下:int ioctl(i ...

  2. kotlin面向对象之接口、代理与委托、单例模式

    接口: 对于什么是接口这里就不概述了,跟java中的概念一样,下面直接上代码进行操练: 而男人跟女人的接口当然也是不同的,很显然男人跟女人最大的差别就是拥有"小弟弟"[我黄我暴利] ...

  3. pthread_cond_wait

    while(1) 33 { 34 mm* p = NULL; 35 pthread_mutex_lock(&mutex); 36 while(head == NULL) 37 pthread_ ...

  4. 调试python 程序的几种方法总结

    程序能一次写完并正常运行的概率很小,基本不超过1%.总会有各种各样的bug需要修正.有的bug很简单,看看错误信息就知道,有的bug很复杂,我们需要知道出错时,哪些变量的值是正确的,哪些变量的值是错误 ...

  5. 数组扩展运算符 -ES6

    1.将数组转为以逗号分隔的序列 2.格式 ...[1,2,3 ] 3.若扩展运算符后面是一个空数组,则不产生效果[ ] 4.用于函数参数 function add(x,y) { console.log ...

  6. php上传文件代码解析

    思想:把html的input标签组织成一个数组,然后去重 关键技术涉及的函数 is_dir mkdir move_uploaded_file() 涉及的数组 预定义数组$_FILES 步骤一:检查上传 ...

  7. python---wav音频

    import pyaudio #导入库 import wave #导入wav音频库 import sys from PyQt5.QtWidgets import QApplication, QWidg ...

  8. 百度AI训练营笔记

    参加了两天百度AI训练营,简单记录一下学到的东西 一.知识图谱 知识图谱是让机器具有积累知识.运用知识的本领. 由于目前知识量很大,所以人工标注的方法无法满足,可以采用数据驱动.自底向上的方式自动构建 ...

  9. 数组 slice方法和splice方法的区别

    一.slice() 方法 slice()方法可以从已有的数组中返回选定的元素. 语法: arrayObject.slice(start,end) 参数: start:(截取开始位置的索引,包含开始索引 ...

  10. 让IE8一下兼容CSS3选择器

    来自英国的网页开发工程师Keith Clark 开发了一个JavaScript方案来使IE支持CSS3选择器.该脚本支持从IE5到IE8的各个版本. 首先,您需要下载DOMAssistant脚本和ie ...