C # basics-Introduction to XML reading and simple ORM implementation,
Background: ASP. in the NETMVC4 project. config configurations meet most of the requirements. However, for some specific businesses, we sometimes need to add new configuration files to record configuration information. Therefore, XML file configuration is undoubtedly one of our solutions. The following is a brief introduction to XML reading.
1. xml. linq read xml
1. Create a data. XML file
1 <Customers> 2 <Customer> 3 <Name> Frank </Name> 4 <City> Chengdu </City> 5 <Contact> 2233 </Contact> 6 </Customer> 7 <Customer> 8 <Name> Vincent </Name> 9 <City> USA </City> 10 <Contact> 4455 </Contact> 11 </Customer> 12 </Mers MERs>
2. Create a customer entity
1 public class Customers2 {3 public string Name { get; set; }4 5 public string City { get; set; }6 7 public string Contact { get; set; }8 }
3. System. Xml. Linq domain name. You can easily read the customer information. XDocument. Load read the xml file. Descendants can read the Customer node and return a collection. Then, read data from each Customer node through the select method of Linq and convert the data to a List <Customer> object.
1 var customsers = XDocument.Load("data.xml").Descendants("Customer")2 .Select(x => new Customers()3 {4 Name = x.Element("Name").Value,5 City = x.Element("City").Value,6 Contact = x.Element("Contact").Value,7 }).ToList();
Ii. Simple ORM
The reading of XML is introduced above. Is there a simpler way to read XML. for example, it is similar to the Database ORM framework. the object name is automatically mapped to the object only when it is the same as the select field name of db SQL. We do not need to write code to retrieve the Element one by one. The following is a simple implementation. The following code is just a reference
In fact, we can use reflection and tagging. If you have time, let's take a look at the two tag and reflection articles written by the younger brother.
C # basic --- Attribute and reflect applications
C # basic --- Attribute and reflect (reflection) Application 2
1. Create an XmlAttribute class. The ElementName attribute is provided to mark xml nodes corresponding to object class fields.
1 public class XmlAttribute : Attribute2 {3 public string ElementName4 {5 get;6 set;7 }8 }
2. Modify the xml node name corresponding to Customler class. ElementName, because the developer's xml node name and object name may be different.
1 public class Customers 2 { 3 4 [Xml(ElementName = "Name")] 5 6 public string Name { get; set; } 7 8 [Xml(ElementName = "City")] 9 10 public string City { get; set; }11 12 [Xml(ElementName = "Contact")]13 14 public string Contact { get; set; }15 }
3. Provides the XmlReader class for reading xml data.
1. Read objects through reflection, such as the field names of the Customer class and the corresponding xml node names, and store them in the dic dictionary.
2. traverse the field and assign the value to the object.
1 public class XmlReader 2 {3 public static List <T> ReadList <T> (string filePath, string rootName) where T: class, new () 4 {5 List <PropertyInfo> propertyInfos = (T) Activator. createInstance (typeof (T ))). getType (). getProperties (). toList (); 6 List <T> result = new List <T> (); 7 var xmlData = XDocument. load (filePath); 8 9 // storage object property name -- key-value of the xml node name 10 Dictionary <string, string> dic = new Dictionary <String, string> (); 11 propertyInfos12. ForEach (property => 13 {14 var attribute = property. GetCustomAttribute (typeof (XmlAttribute) as XmlAttribute; 15 if (attribute! = Null) 16 {17 dic. add (property. name, attribute. elementName); 18} 19}); 20 21 return xmlData22. descendants (rootName) 23. select (element => 24 {25 // instantiate an object 26 var info = (T) Activator. createInstance (typeof (T); 27 28 // traverses the keyvalue field and assigns 29 foreach (KeyValuePair <string, string> pair in dic) to each field of the object) 30 {31 // read the object's filed tag 32 var attr = info. getType (). getProperty (pair. key); 33 attr. setValue (info, element. element (pair. value ). value); 34} 35 return info; 36 }). toList (); 37} 38}
4. Let's take a look at the results and read the data easily.
Iii. Summary
This article is just a simple introduction to XML reading. There are many types of xml, such as in the case of <customer name = 'frank'/>. This type of situation should be encapsulated again. You need to add an AttributeName to implement the XmlAttribute class. Besides string, int, datetime, and other data types. Xml nesting, multi-layer parsing of xml data, and so on. There should be a lot of xml reading frameworks on the Internet. I believe many companies will also have their own xml reading frameworks. Although there is no need to recreate the wheel. But as a developer, you can still think about it. If you want to develop these public frameworks, you should also know how to do it and how to implement it.
For more C # basic notes, click the link below: http://www.cnblogs.com/FourLeafCloverZc/tag/C%23%E5%9F%BA%E7%A1%80%E7%AC%94%E8% AE %B0/