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文件的读取的更多相关文章

  1. spring 框架的xml文件如何读取properties文件数据

    spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...

  2. python之xml 文件的读取方法

    ''' xml 文件的读取方法 ''' #!/usr/bin/env python # -*- coding: utf- -*- import xml.etree.ElementTree as ET ...

  3. XML文件的读取----cElementTree

    XML文件如下: <?xml version="1.0" encoding="UTF-8"?> <tokenxml> <token ...

  4. Java文件操作①——XML文件的读取

    一.邂逅XML 文件种类是丰富多彩的,XML作为众多文件类型的一种,经常被用于数据存储和传输.所以XML在现今应用程序中是非常流行的.本文主要讲Java解析和生成XML.用于不同平台.不同设备间的数据 ...

  5. 使用DOM4J解析XMl文件与读取XML文件

    XML文件 <?xml version="1.0" encoding="UTF-8"?> <bookstore> <book id ...

  6. C#的XML文件的读取与写入

    在设计程序的时候,对于一些变化性较强的数据,可以保存在XML文件中,以方便用户修改.尤其是对于一些软硬件的配置文件,很多都选择了用XML文件来存取.XML文件简单易用,而且可以在任何应用程序中读写数据 ...

  7. WinForm中DataGridView对XML文件的读取

    转自http://www.cnblogs.com/a1656344531/archive/2012/11/28/2792863.html c#读取XML   XML文件是一种常用的文件格式,例如Win ...

  8. XML文件的读取

    <?xml version="1.0" encoding="gbk"?> <!--设置编码格式为gbk--> <!DOCTYPE ...

  9. linux下使用libxml2实现对xml文件的读取及查询

    由于项目需要,这两天在用C++做XML文件解析的工作.在linux下有个很方便的操作xml文件的库——libxml2,它提供了一套创建和查询xml文件的C语言的接口.这篇博客主要介绍如何使用libxm ...

随机推荐

  1. C中位域的使用

    一.位域 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C语言又提供了一 ...

  2. forward和redirect 的区别

    一.使用区别 1.从地址栏显示来说 forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器.浏览器根本不知道服务器发送的内容从哪里来 ...

  3. openstack liberty 版本按照官方文档手动整合 完成后 基于dashboard-horizon 创建虚拟机报错 用CL却是成功的 网络等验证都是正确的通过启动的虚拟实例测试以成功

  4. 安装XCode导致mac无法正常开机怎么办

    作为一个IOS编程菜鸟,之前在网上买的IOS教程都坑爹的是xcode4以前的版本的,由于3和4界面变化较大,所以看着书往往不知所云,所以就迫不及待地从网上下载了XCode 3.2.5+iOS SDK ...

  5. array_column php 函数

    今天想从二维数组中取某个列成一维数组 结果发现 array_column需要php5.5支持才行 然后自己写了一个仿造版本的array_column 两种思路 1.直接遍历 取值给追加数组 返回 2. ...

  6. 在storyboard中的静态UITableView中拖入 UISearchBar and Search Display Controller出现的奇怪问题

         近期学习过程中想模拟一下新浪微博"发现"界面.      我在storyboard中拖入一个UITableViewController,设置这个UITableViewCo ...

  7. vim的全局替换

    本文出自   http://blog.csdn.net/shuangde800 本文是在学习<使用vi编辑器, Lamb & Robbins编著>时在所记的笔记. 本文内容: 基本 ...

  8. DTRACE 专家

    http://dtrace.org/blogs/bmc/ https://github.com/bcantrill http://www.tudou.com/programs/view/Q6fHZFg ...

  9. lua function

    This is the main reference for the World of Warcraft Lua Runtime. Note that these are mostly standar ...

  10. 从源码角度深入理解Handler

    为了获得良好的用户体验,Android不允许开发者在UI线程中调用耗时操作,否则会报ANR异常,很多时候,比如我们要去网络请求数据,或者遍历本地文件夹都需要我们在新线程中来完成,新线程中不能更新UI, ...