读取xml文件转成List<T>对象的两种方法
2013-12-12 17:59
691 查看
点击打开链接
读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法,加上自己知道的另一种实现方法。
就以一个简单的xml做例子。
xml格式如下:
Product对象如下:
要实现的就是要把xml文件的内容读取出来转成List<Product>对象,需求明白了,那接下来就来介绍实现的方法。
一、利用.net中的XmlSerializer类提供的方法
1、首先要在Product、Products类中的每个属性上加上与xml对应的描述字段,如下代码:
注意AttributeName一定要和xml中的一致。
2、相应的对应关系建立好了之后,下面就来进行读取反序列化,代码如下:
这个方法里也没什么特别的就是先读取.xml内容,然后再反Deserialize方法反序化xml内容转成Products。
这种方法大致就这么简单,我个人是比较倾向于这种方法的,因为它不用自己去解析xml中相应的属性等内容,也比较灵活,xml中的属性名变了,在类中相应的属性上改一下AttributeName的值就可以了。
读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法,加上自己知道的另一种实现方法。
就以一个简单的xml做例子。
xml格式如下:
<?xml version="1.0" encoding="utf-8" ?> <products> <product name="West Side Story" price="9.99" supplierId="1" /> <product name="Assassins" price="14.99" supplierId="2" /> <product name="Frogs" price="13.99" supplierId="1" /> <product name="Sweeney Todd" price="10.99" supplierId="3" /> </products>
Product对象如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; namespace XmlToList1 { public class Product { [XmlAttribute(AttributeName = "name")] public string Name { get; set; } [XmlAttribute(AttributeName = "price")] public decimal Price { get; set; } [XmlAttribute(AttributeName = "supplierId")] public decimal SupplierId { get; set; } } }
要实现的就是要把xml文件的内容读取出来转成List<Product>对象,需求明白了,那接下来就来介绍实现的方法。
一、利用.net中的XmlSerializer类提供的方法
1、首先要在Product、Products类中的每个属性上加上与xml对应的描述字段,如下代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; namespace XmlToList1 { public class Product { [XmlAttribute(AttributeName = "name")] public string Name { get; set; } [XmlAttribute(AttributeName = "price")] public decimal Price { get; set; } [XmlAttribute(AttributeName = "supplierId")] public decimal SupplierId { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; namespace XmlToList1 { //[XmlRoot("products")] [XmlRoot("products")] public class Products { [XmlElement("product")] //public Product[] Items { get; set; } public Product[] Items//Product为类名字 { get; set; } } }
注意AttributeName一定要和xml中的一致。
2、相应的对应关系建立好了之后,下面就来进行读取反序列化,代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; using System.IO; namespace XmlToList1 { public class LoadXml { //private static IList<Product> products = new List<Product>(); private static IList<Product> products = new List<Product>(); static LoadXml() { try { using (TextReader reader = new StreamReader("data.xml")) { var ser = new XmlSerializer(typeof(Products)); var items = (Products)ser.Deserialize(reader); if (items != null) { products = items.Items; } } } catch (System.Exception ex) { Console.WriteLine("出错了," + ex.Message); } } public static IList<Product> GetProducts() { return products; } } }
这个方法里也没什么特别的就是先读取.xml内容,然后再反Deserialize方法反序化xml内容转成Products。
这种方法大致就这么简单,我个人是比较倾向于这种方法的,因为它不用自己去解析xml中相应的属性等内容,也比较灵活,xml中的属性名变了,在类中相应的属性上改一下AttributeName的值就可以了。
相关文章推荐
- 读取xml文件转成List<T>对象的两种方法(附源码)
- 读取xml文件转成List<T>对象的两种方法
- 读取xml文件转成List<T>对象的两种方法(附源码)
- (转) 读取xml文件转成List<T>对象的两种方法
- 取xml文件转成List<T>对象的两种方法
- Dom4J解析xml文件动态转换为List<Bean>或者Map集合
- Spring事务管理+openJPA List<实体>方法更新所有对象
- web.xml文件加载顺序 一、 1 、启动一个 WEB 项目的时候, WEB 容器会去读取它的配置文件 web.xml ,读取 <listener> 和 <context-param>
- vector<char> 容器初始化 string 对象 两种方法
- java 从jar中读取文件 三种方法<Enumeration>
- 关于web.xml中的<welcome-file-list>中的默认首页文件
- 关于web.xml中的<welcome-file-list>中的默认首页文件
- JS 使用List<Object> 对象中的Object对象的某个属性的临时处理方法
- Android 将List<Bean>数据写入文件,从文件中读取;以及多个 Activity 的退出方法
- Android-->AndroidManifest.xml 文件 <meta-data> 使用方法
- Java读取TXT文件方法,和检索链接<a>[url]
- 使用Map把sql返回的list<Object>转成需要的实体对象
- mybatis返回类型string,或者对象,均可用list<string>或者,list<对象>来接收
- 在AndroidManifest.xml文件中<activity>的android:windowSoftInputMode属性使用
- asp读取xml的两种方法(读取固定xml文件和读取url地址返回的xml)