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. vijosP1037搭建双塔

    vijosP1037搭建双塔 链接:https://vijos.org/p/1037 [思路] DP. [代码] #include<iostream> #include<cstrin ...

  2. HTML5 Canvas核心技术—图形、动画与游戏开发.pdf5

    文本的定位 水平与垂直定位:当使用strokeText()和fillText()绘制文本时,指定了所绘文本的X与Y坐标,还有textAlign与textBaseline两个属性 textAlign:s ...

  3. 5 approach to load UIView from Xib

    After the past few years I found that the only manageable way for creating/maintaining view (or any ...

  4. 322. Coin Change

    动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...

  5. Mockito测试

    Mockito 一 mockito基本概念 Mock测试是单元测试的重要方法之一,而Mockito作为一个流行的Mock框架,简单易学,且有非常简洁的API,测试代码的可读性很高. Mock测试就是在 ...

  6. ReactiveCocoa框架学习2

    昨天内容回顾 信号类:表示有数据产生,信号类不发送数据 注意:不同的信号,订阅方式不同 RACSignal 创建RACDynamicSignal信号 -> didSubscribe(block) ...

  7. PAT 1070. Mooncake (25)

    Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival.  Many types ...

  8. 百度的android采访分析

    今天早上10分,我接到一个电话采访百度.当然,我说提前.我的病是多么强烈延迟.所以我也没怎么准备,当然,我也看他们的真实水平的思考.在这次审查中! ! ! ! ! !! .! ! ! !.!! !.! ...

  9. Android实战技巧之六:PreferenceActivity使用详解

    一.写作前面 当我们做应用的时候,需要用户配置一些信息,而这就是通常所说的应用设置. 对于Android系统来说,系统本身的设置带来的用户体验和习惯已经深入人心,在我们的应用中同样用到类似的设置页, ...

  10. JAVA的程序代码小细节,变量的使用,以及一些细节的面试题

    package cn.hncu; public class LableDemo { public static void main(String[] args) { //demo1(); demo2( ...