诚如他第22楼“只因渴求等待”提出的疑问一样,他的下面那一段代码是存在一点点问题的,

XElement root = XElement.Load(fileName);
var objects = from obj in root.Elements("object") select obj;

如果照搬照抄刘冬大侠的这段代码那是不会成功读取数据的,窃以为这应该是刘冬大侠故意埋的一雷吧。

根据他的文章,我实践了一遍:

先创建了几个类,一个Person类; 一个Man类; 一个Woman类,一共3个类,后面会将根据这个三个类创建xml文档;

Person:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace IocEasy
{
public class Person
{
public string Name { get; set; }
public string Sex { get; set; } public Person()
{
} public void Eat(string something)
{
Console.WriteLine(something);
} public void MakeLove(Person person)
{
switch (person.Sex)
{
case "男": Console.WriteLine(this.Name + "和" + person.Name + "只能搞基"); break;
case "女": Console.WriteLine(this.Name + "和" + person.Name + "可以相爱"); break;
default: break;
}
} }
}

Man:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace IocEasy
{
public class Man : Person
{
public Man()
{
} public Man(string name, string sex)
{
base.Name = name;
base.Sex = sex;
}
}
}

Woman:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace IocEasy
{
public class Woman : Person
{
public Woman()
{
} public Woman(string name, string sex)
{
base.Name = name;
base.Sex = sex;
}
}
}

接下来就根据上面三个类(随手写的)创建xml文档,

Object.xml:

<?xml version="1.0" encoding="utf-8" ?>

<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net
http://www.springframework.net/xsd/spring-objects.xsd">
<object id="Person" type="IocEasy.Person,IocEasy" ></object>
<object id="Man" type="IocEasy.Man,IocEasy" ></object>
<object id="Woman" type="IocEasy.Woman,IocEasy" ></object> </objects>

跟着就是XmlFcatory类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Data;
using System.Xml; namespace IocEasy
{
public class XmlFactory
{
private IDictionary<string, object> objectDefine = new Dictionary<string, object>(); public XmlFactory(string fileName)
{
InstanceObjects(fileName);
} private void InstanceObjects(string fileName)
{
XNamespace ns = "http://www.springframework.net";
XName name = ns + "object";
XElement root = XElement.Load(fileName);
var objects = from obj in root.Elements(name) select obj;
objectDefine = objects.ToDictionary(
k => k.FirstAttribute.Value,
v =>
{
string typeName = v.Attribute("type").Value;
Type type = Type.GetType(typeName);
return Activator.CreateInstance(type);
}
);
} public object GetObject(string id)
{
object result = null;
if (objectDefine.ContainsKey(id))
{
result = objectDefine[id];
}
return result;
} }
}

最后在就是主程序入口处调用了:

Program类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace IocEasy
{
class Program
{
static void Main(string[] args)
{
AppRegister();
Console.ReadLine();
} static void AppRegister()
{
XmlFactory ctx = new XmlFactory(@"C:\Documents and Settings\Administrator\桌面\IocEasy\IocEasy\Object.xml");
Person p1 = ctx.GetObject("Man") as Person;
p1.Name = "Euler";
p1.Sex = "男";
Person p2 = ctx.GetObject("Woman") as Person;
p2.Name = "Echo";
p2.Sex = "女"; p1.Eat(p1.Name + "喜欢抽烟");
p2.Eat(p2.Name + "喜欢旅行"); p1.MakeLove(p2);
}
}
}

这是一个完整的实践,只不过其中的谬误稍作修改罢了。

修改的代码如下:

private void InstanceObjects(string fileName)
{
XNamespace ns = "http://www.springframework.net";
XName name = ns + "object";
XElement root = XElement.Load(fileName);
var objects = from obj in root.Elements(name) select obj;
objectDefine = objects.ToDictionary(
k => k.Attribute("id").Value,//k.FirstAttribute.Value,
v =>
{
string typeName = v.Attribute("type").Value;
Type type = Type.GetType(typeName);
return Activator.CreateInstance(type);
}
);
}

如此才能正常读取数据。

关于刘冬大侠Spring.NET系列学习笔记3的一点勘正的更多相关文章

  1. MVA Universal Windows Apps系列学习笔记1

    昨天晚上看了微软的Build 2015大会第一天第一场演讲,时间还挺长,足足3个小时,不过也挺震撼的.里面提到了windows 10.Microsoft edge浏览器.Azure云平台.Office ...

  2. 【转】Spring.NET学习笔记——目录

    目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...

  3. 《Machine Learning》系列学习笔记之第一周

    <Machine Learning>系列学习笔记 第一周 第一部分 Introduction The definition of machine learning (1)older, in ...

  4. Spring MVC 学习笔记一 HelloWorld

    Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...

  5. SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能

    在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...

  6. SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证

    整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...

  7. 第63节:Java中的Spring MVC简介笔记

    前言 感谢! 承蒙关照~ Java中的Spring MVC简介笔记 MVC简介 Spring MVC 基本概念 Spring MVC 项目搭建 maven 使用Spring MVC进行开发 实现数据绑 ...

  8. spring揭密学习笔记

    spring揭密学习笔记 spring揭密学习笔记(1) --spring的由来 spring揭密学习笔记(2)-spring ioc容器:IOC的基本概念

  9. Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建

    Spring MVC 学习笔记12 -- SpringMVC+Hibernate开发(1)依赖包搭建 用Hibernate帮助建立SpringMVC与数据库之间的联系,通过配置DAO层,Service ...

随机推荐

  1. 垂直的TextView

    所先声明一下这个类是我从网上找到的一篇文章,只是保留并没有侵权的意思. public class TextViewVertical extends View { public static final ...

  2. Math.random();函数 随机数

    random函数参数 无参数 random函数返回值 返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1) random函数示例 document.write(Math.random()); ...

  3. input框内默认文字点击消失

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. C# "error CS1729: 'XXClass' does not contain a constructor that takes 0 arguments"的解决方案

    出现这种错误的原因时,没有在子类的构造函数中指出仅有带参构造函数的父类的构造参数. 具体来讲就是: 当子类要重用父类的构造函数时, C# 语法通常会在子类构造函数后面调用 : base( para_t ...

  5. [偏微分方程教程习题参考解答]4.1Duhamel 原理

    1. 如果已知下述常微分方程的特定初值问题 $$\bex \sedd{\ba{ll} -y''+y=0,&x>0,\\ y(0)=0,\quad y'(0)=1 \ea} \eex$$ ...

  6. Android实例] android获取web服务器端session并验证登陆

    传统网页实现用户登陆一般采用session或cookie记录用户基本信息又或者两者结合起来使用.android也可以采用session实现用户登陆验证并记录用户登陆状态时的基本信息,session是在 ...

  7. hdu 1527 取石子游戏(Wythoff Game)

    题意:Wythoff Game 思路:Wythoff Game #include<iostream> #include<stdio.h> #include<math.h& ...

  8. 第一个UI脚本--python+selenium

    之前一直是用java+selenium做自动化测试的,最近因为工作需要,需要用pyhton+selenium去实现,于是就赶驴上架,熟悉了一下python的语法和脚本的编写过程,下面是一个简单的脚本, ...

  9. 获取 windows地址栏 网页地址栏 文件名

    int start=str.LastIndexOf("\\"); fileName=str.SubString(start+1,str.Length-start-1); int s ...

  10. 俄罗斯方块游戏 --- java

    俄罗斯方块游戏 如有疑问请查看:http://zh.wikipedia.org/zh-tw/%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97 更多疑问请参考: ...