第二种: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. Delphi Opendialog组件

  2. Delphi BitBtn组件

  3. yarn查看正在运行的任务列表

    1 yarn application -list

  4. linux进程间的通信方式

    linux进程间的通信 进程间的通信就是不同的进程之间传播或交换信息,进程的用户空间是互相独立,进程之间可以利用系统空间交换信息. 管道 允许将一个进程的标准输出和另一个进程的标准输入连接在一起,主要 ...

  5. jQuery获取当前按钮的id

    function test(e) { $(e.target).attr('id') } // 记得带上点击事件

  6. magento购物车添加减少数量 实时更新购物车

    magento实时更新购物车商品数量 <td> <button onclick="addQtyone();" type="submit"> ...

  7. 利用pipework为docker容器设置固定IP

    今天介绍如何在redhat/centos7系列机器上使用pipework为docker启动的容器指定一个固定ip,我们知道默认情况下,docker会使用 bridge网络模式为每一个启动的容器动态分配 ...

  8. C#制作的屏幕取色器

    1 using System;  2 using System.Collections.Generic;  3 using System.ComponentModel;  4 using System ...

  9. k8sService资源

    一.service资源及其实现模型 通过规则定义出由多个pod对象组合而成的逻辑集合,以及访问这组pod的策略.service关联pod资源的规则要借助于标签选择器来完成 1.service资源概述 ...

  10. 一个tornado框架下的文件上传案例

    html部分----使用了form表单,注意三要素 method="post"  action="/loaddata" enctype="multip ...