Two Methods for reading xml files and converting them to List & lt; T & gt; objects (source code included)

Source: Internet
Author: User
Tags xml example

Reading xml files is frequently used in projects. So let's sum up the methods used in the recent project to read xml files and convert them into List <T> objects, add another implementation method that you know. Let's use a simple xml example. The xml format is as follows: 1 <? Xml version = "1.0"?> 2 <products> 3 <product name = "West Side Story" price = "9.99" supplierId = "1"/> 4 <product name = "Assassins" price = "14.99" supplierId = "2"/> 5 <product name = "Frogs" price = "13.99" supplierId = "1"/> 6 <product name = "Sweeney Todd" price = "10.99" supplierId = "3"/> 7 </products> the Product object is as follows: 1 public class Product2 {3 public string Name {get; set;} 4 5 public decimal Price {get; set;} 6 7 public decimal Suppli ErId {get; set;} 8} is to read the content of the xml file and convert it into a List <Product> object. The requirement is clear, next we will introduce the implementation method. I. Exploitation. method 1 provided by the XmlSerializer class in. net. First, add the description field corresponding to xml to each attribute in the Product and Products classes. The Code is as follows: [XmlRoot ("products")] public class Products {[XmlElement ("product")] public Product [] Items {get; set ;}} 1 public class Product 2 {3 [XmlAttribute (AttributeName = "name")] 4 public string Name {get; set;} 5 6 [XmlAttribute (AttributeName = "price")] 7 public decimal Price {get; set;} 8 9 [XmlAttribute (Attribute Name = "supplierId")] 10 public decimal SupplierId {get; set;} 11} note that AttributeName must be consistent with that in xml. 2. After the corresponding relationship is established, read the deserialization Code as follows: 1 private static IList <Product> products = new List <Product> (); 2 static LoadXml () 3 {4 try 5 {6 using (TextReader reader = new StreamReader ("data. xml ") 7 {8 var serializer = new XmlSerializer (typeof (Products); 9 var items = (Products) serializer. deserialize (reader); 10 if (items! = Null) 11 {12 products = items. items; 13} 14} 15} 16 catch (Exception ex) 17 {18 Console. writeLine ("error," + ex. message); 19} 20} In this method, nothing special is to read first. xml content, and then reverse the Deserialize method to reverse the xml content into Products. This method is generally so simple. I personally prefer this method, because it does not have to parse the corresponding attributes in xml, and is flexible. The attribute names in xml have changed, modify the value of AttributeName in the corresponding attributes of the class. 2. Using linq for conversion this will be known as the linq estimation. The Code is as follows: 1 private static IList <Product> products = new List <Product> (); 2 static LoadXml () 3 {4 try 5 {6 XDocument doc = XDocument. load ("data. xml "); 7 products = 8 doc. descendants ("product") 9. select (10 x => 11 new Product12 {13 Name = x. attribute ("name "). toString (), 14 Price = (decimal) x. attribute ("price"), 15 SupplierId = (long) x. attribute ("supplierId") 16}) 17. toList (); 18} 19 catch (Exception ex) 20 {21 Console. writeLine ("error," + ex. message); 22} 23}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.