MVVM模式应用 之xml文件的读取
XML如下所示:
<?xml version="1.0" encoding="utf-8" ?> <schools> <school id='1'> <SchoolName>郑州大学</SchoolName> <SchoolClass>一本</SchoolClass> </school> <school id='2'> <SchoolName>河南大学</SchoolName> <SchoolClass>一本</SchoolClass> </school> <school id='3'> <SchoolName>南阳理工学院</SchoolName> <SchoolClass>二本</SchoolClass> </school> <school id='4'> <SchoolName>河南工业大学 </SchoolName> <SchoolClass>二本</SchoolClass> </school> </schools>
现在,需要把该xml文件里面的内容读取出来。
(1)首先,新建一个类University.CS,放在Model文件夹中
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace 河南大学学府.Model { /// <summary> /// 大学类 /// </summary> public class University { public int SchoolID { get; set; } public string SchoolName { get; set; } public string SchoolClass { get; set; } } }
(2)
先定义一个接口IUniversity,放在文件夹Service中
using System; using System.Collections.Generic; using System.Linq; using System.Text; using 河南大学学府.Model; namespace 河南大学学府.Service { interface IUniversity { List<University> GetUniversityList(); } }
(3)
创建类UniversityService并且继承接口IUniversity
我们把读取的内容放到List<University>里面
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Collections.Generic; using 河南大学学府.Model; using System.Xml.Linq; using System.Linq; namespace 河南大学学府.Service { public class UniversityService:IUniversity { /// <summary> /// 得到河南大学列表 /// </summary> /// <returns></returns> public List<University> GetUniversityList() { //解析文件University.xml XDocument doc = new XDocument(); doc = XDocument.Load("/Data/University.xml"); List<University> UniversityList = new List<University>(); UniversityList = (from db in doc.Element("schools").Elements("school") select new University { SchoolID = Int32.Parse( db.Attribute("id").Value), SchoolName = db.Element("SchoolName").Value, SchoolClass = db.Element("SchoolClass").Value }).ToList(); return UniversityList; } } }
这样,根据University的GetUniversityList方法就能得到我们需要的数据了。
MVVM模式应用 之xml文件的读取的更多相关文章
- spring 框架的xml文件如何读取properties文件数据
spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...
- python之xml 文件的读取方法
''' xml 文件的读取方法 ''' #!/usr/bin/env python # -*- coding: utf- -*- import xml.etree.ElementTree as ET ...
- XML文件的读取----cElementTree
XML文件如下: <?xml version="1.0" encoding="UTF-8"?> <tokenxml> <token ...
- Java文件操作①——XML文件的读取
一.邂逅XML 文件种类是丰富多彩的,XML作为众多文件类型的一种,经常被用于数据存储和传输.所以XML在现今应用程序中是非常流行的.本文主要讲Java解析和生成XML.用于不同平台.不同设备间的数据 ...
- 使用DOM4J解析XMl文件与读取XML文件
XML文件 <?xml version="1.0" encoding="UTF-8"?> <bookstore> <book id ...
- C#的XML文件的读取与写入
在设计程序的时候,对于一些变化性较强的数据,可以保存在XML文件中,以方便用户修改.尤其是对于一些软硬件的配置文件,很多都选择了用XML文件来存取.XML文件简单易用,而且可以在任何应用程序中读写数据 ...
- WinForm中DataGridView对XML文件的读取
转自http://www.cnblogs.com/a1656344531/archive/2012/11/28/2792863.html c#读取XML XML文件是一种常用的文件格式,例如Win ...
- XML文件的读取
<?xml version="1.0" encoding="gbk"?> <!--设置编码格式为gbk--> <!DOCTYPE ...
- linux下使用libxml2实现对xml文件的读取及查询
由于项目需要,这两天在用C++做XML文件解析的工作.在linux下有个很方便的操作xml文件的库——libxml2,它提供了一套创建和查询xml文件的C语言的接口.这篇博客主要介绍如何使用libxm ...
随机推荐
- 排列的Java递归语言实现
在做算法题的时候,发现排列经常被使用到,是一个重要的知识点, 下面是博主修改过的代码,初学者,如有不足,欢迎指出 import java.util.ArrayList; import java.uti ...
- iPhone/Mac Objective-C内存管理教程和原理剖析
http://www.cocoachina.com/bbs/read.php?tid-15963.html 版权声明 此文版权归作者Vince Yuan (vince.yuan#gmail.com)所 ...
- 【转】shell 教程——03 Shell脚本语言与编译型语言的差异
大体上,可以将程序设计语言可以分为两类:编译型语言和解释型语言. 编译型语言 很多传统的程序设计语言,例如Fortran.Ada.Pascal.C.C++和Java,都是编译型语言.这类语言需要预先将 ...
- tensorflow 保存变量,
代码: #!usr/bin/env python# coding:utf-8"""这个代码的作用是 通过 tensorflow 来计算 y = 0.3x + 0.1 的线 ...
- 内容提供者(Content Provider)——跨程序共享数据
内容提供者 Content Provider 应用的数据库是不允许其他应用访问的 内容提供者的作用就是让别的应用访问到你的数据库 自定义内容提供者,继承ContentProvider类,重写增删改查方 ...
- 关于ABAP事件的一张图
事件: 这里有几组事件关键字 ,这些事件关键字在特定环境下控制ABAP/4 程序流. 逻辑数据库 是典型报表程序的外部流控制的中心点.如果将逻辑数据库链接到报表 程序,将导致显示选择 屏幕,并决定系统 ...
- c# 扩展方法奇思妙用集锦
本文转载:http://www.cnblogs.com/ldp615/archive/2009/08/07/1541404.html 其中本人觉得很经典的:c# 扩展方法奇思妙用基础篇五:Dictio ...
- android关机充电
1.关机充电其实是进入adb shell很快的方式! 2.手机关机时候插入USB,手机将进入关机充电模式,那么这个模式究竟是怎么进行的,这里分析如下! (1)uboot:这里代码大概浏览了一下:u-b ...
- jquerymobile知识点三:弹出层popup
弹出层popup很简单,主要就是弹出验证,登陆注册,提交信息之类的,下面是我写好的一个demo... <div data-role="popup" id="popu ...
- poj 1226
跟3294比较类似,但是不需要输出具体的串,比较简单,只要把串反转连接上去解法就一样了. #include <iostream> #include <cstdio> #incl ...