诚如他第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); } ); }
如此才能正常读取数据。