第二种: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. openstack mitaka开启三层网络vxlan

    在这之前,先把之前基于flat模式创建的虚机,全部删除 控制节点: 配置 修改/etc/neutron/neutron.conf的[DEFAULT]区域 将 core_plugin = ml2 ser ...

  2. CDN和浏览器缓存

    1,CDN 旨在解决的最重要的问题是什么,我们称之为网络延迟,通过网络获取资源总是比从本地获取慢,无论服务器是在同一个局域网中还是位于世界的另一个角落,都是如此.这里的速度差异是 IT 行业的一个核心 ...

  3. 简单了解 TCP TCP/IP HTTP HTTPS

    一. 什么是TCP连接的三次握手 第一次握手:客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确认: 第二次握手:服务器收到syn包,必须确认客户的SYN(ack=j+ ...

  4. 学习Linux的准备

    学习方式: 主动学习: 动手实践:40% 讲给别人:70% 被动学习: 听课:10% 笔记:20% 写博客的要求: 写博客是对某一方面知识的总结,输出:是知识的书面化的表达方式.写博客不同于写笔记,笔 ...

  5. zencart产品属性dropmenu select只有一个选择项时自动变成radio单选的解决办法

    includes\modules\classic\attributes.php 在大约786行代码 case ($products_options->RecordCount() == 1): 的 ...

  6. uestc summer training #3 线段树优化建边

    线段树建边 struct E { int value, modvalue; } a[MAXN << ]; pair<int, int> b[MAXN]; ], r[MAXN & ...

  7. centos后台运行程序

    putty等软件运行,python程序:python p.py    只要一关闭putty, 程序就结束.如何让退出终端或关闭终端电脑,还能让程序在服务器后台运行Python. 关键的命令:nohup ...

  8. fixed固定时弹窗上的input被软键盘遮住的问题

    <div class="pwhCover"> <div class="pwhCon"> <div class="pwhT ...

  9. ssm框架使用详解&配置两个数据源

    学习ssm框架已经快一年了,今天把这个框架总结一下. SSM 就是指 spring.SpringMVC和Mybatis.先说一下基本概念(百度上搜的) 1.基本概念 1.1.Spring Spring ...

  10. Acwing-120-防线(二分,前缀和)

    链接: https://www.acwing.com/problem/content/122/ 题意: 达达学习数学竞赛的时候受尽了同仁们的鄙视,终于有一天......受尽屈辱的达达黑化成为了黑暗英雄 ...