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 ...
随机推荐
- 2013 ACM区域赛长沙 K Pocket Cube hdu 4801
题意:给了一个2*2的魔方..每步操作可以将任意一面翻转90度..现在问在N(<=7)步内.最多能翻出几面相同的. 直接打表模拟每种翻转情况 #include<cstdio> #in ...
- POJ 3666 Making the Grade (DP滚动数组)
题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...
- Vi和Vim的区别及联系
它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面.vim的这些优势主要体现在以下几个方面:1.多级撤消我们知道在vi里,按 u只能撤消上次命 ...
- tomcat多域名配置
1.找到你的tomcat然后在conf文件中找到server.xml打开: <Server port="8005" shutdown="SHUTDOWN" ...
- CAS SSO:汇集配置过程中的错误解决方法
本教程为gevin.me原创文章,转载请注明: CAS SSO:配置过程中的错误解决方法 | Gevin’s Blog 本文将收集在配置CAS SSO遇到的所有错误,希望对大家有帮助,也方便下次搭建的 ...
- 在java项目中应用ueditor
虽然百度ueditor的官网和文档都已经很详细了.但是自己还是记录下 自己使用uEditor的过程. 这是 他的官网 http://ueditor.baidu.com/website/ 例子 文档什 ...
- Python 3语法小记(六)条件、循环和assert、pass、del
条件: if 条件: 语句块 elif: 语句块 else: 语句块 elif 表示 else if 这居然是合法的!!!1 < x < 2!!! >> ...
- Linux 内核开发—内核简单介绍
内核简单介绍 Linux 构成 Linux 为什么被划分为系统空间和内核空间 隔离核心程序和应用程序,实现对核心程序和数据的保护. 什么内核空间,用户空间 内核空间和用户空间是程序执行的两种不同的状态 ...
- 一行代码解决Android M新的运行时权限问题
Android M运行时权限是个啥东西 啥是运行时权限呢?Android M对权限管理系统进行了改版,之前我们的App需要权限,只需在manifest中申明即可,用户安装后,一切申明的权限都可来去自如 ...
- park、unpark、ord 函数使用方法(转)
park,unpark,ord这3个函数,在我们工作中,用到它们的估计不多. 我在最近一个工作中,因为通讯需要用到二进制流,然后接口用php接收.当时在处理时候,查阅不少资料.因为它们使用确实比较少, ...