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 ...
随机推荐
- 字符编码笔记:ASCII,Unicode和UTF-8,附带 Little endian和Big endian的解释
作者: 阮一峰 日期: 2007年10月28日 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问题比我想象的复杂,从午饭后一直看到晚上9点,才算初步 ...
- 从源码编译rpi的内核
Kernel Building https://www.raspberrypi.org/documentation/linux/kernel/building.md There are two mai ...
- 缓存算法–LRU
LRU LRU是Least Recently Used 的缩写,翻译过来就是“最近最少使用”,也就是说,LRU缓存把最近最少使用的数据移除,让给最新读取的数据.而往往最常读取的,也是读取次数最多的,所 ...
- PAT 1017. Queueing at Bank
Suppose a bank has K windows open for service. There is a yellow line in front of the windows which ...
- linux 环境下java环境配置
vi + /etc/profile向文件里面追加以下内容:JAVA_HOME=/usr/local/java7JRE_HOME=/usr/local/java7/jrePATH=$PATH:$JAVA ...
- SQL Server存储内幕系列
http://blog.itpub.net/355374/list/1/?cid=75087
- careercup-数组和字符串1.5
1.5 利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能.比如,字符串”aabcccccaaa“会变成”a2b1c5a3“.若”压缩“后的字符串没有变短,则返回原先的字符串. 类似 le ...
- servlet和手动创建servlet,断点调试
1. 什么是Servlet Servlet是一种用Java语言编写的Web应用组件 Servlet主要用于动态网页输出,扩展了Web服务器的功能 Servlet由Servlet容器进行管理 2. ...
- xcode语法高亮插件
转自http://www.cocoachina.com/bbs/read.php?tid=150107 首先感谢该楼主的亲情贡献@不胜感激! 在Xcode4.6.3中,我使用如下方法放到Plugins ...
- CHENEY-YANG'S BLOG(cheney-yang)
This is Cheney-Yang's blog ! Welcome to here ! This is Cheney-Yang's blog ! Welcome to here ! This i ...