码迷,mamicode.com
首页 > 其他好文 > 详细

XML文件操作

时间:2021-04-12 12:30:15      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:drawing   obj   size   uname   com   alt   click   partial   getattr   

XML文件操作

技术图片

 

 

实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XMLDemo
{
    class Student
    { 
        public string StuName { get; set; }
        public int StuAge { get; set; }
        public string Gender { get; set; }
        public string ClassName { get; set; }      
    }
}

 

XML文件

<?xml version="1.0" encoding="utf-8" ?>
<Students>
  <Student>
    <StuName>王永利</StuName>
    <StuAge>19</StuAge>
    <Gender>男</Gender>
    <ClassName>计算机1班</ClassName>
  </Student>
  <Student>
    <StuName>王大拿</StuName>
    <StuAge>22</StuAge>
    <Gender>男</Gender>
    <ClassName>计算机2班</ClassName>
  </Student>
  <Student>
    <StuName>刘军民</StuName>
    <StuAge>21</StuAge>
    <Gender>男</Gender>
    <ClassName>计算机3班</ClassName>
  </Student>
  <Student>
    <StuName>刘莉莉</StuName>
    <StuAge>23</StuAge>
    <Gender>女</Gender>
    <ClassName>计算机4班</ClassName>
  </Student>
  <Student>
    <StuName>赵鑫鑫</StuName>
    <StuAge>20</StuAge>
    <Gender>女</Gender>
    <ClassName>计算机5班</ClassName>
  </Student>
  <DataInfo>
    <Version vNo="1.2" pTime="2012-12-12">数据版本信息</Version>
  </DataInfo>
</Students>

 

主窗体程序

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Xml;//引入命名空间

namespace XMLDemo
{
    public partial class FrmReadXML : Form
    {
        public FrmReadXML()
        {
            InitializeComponent();
        }
        private void btnLoadXML_Click(object sender, EventArgs e)
        {
            XmlDocument objDoc = new XmlDocument();  //【1】创建XML文档操作对象
            objDoc.Load("StuScore.xml");  //【2】加载XML文件到文档对象中
            XmlNode rootNode = objDoc.DocumentElement;  //【3】获取XML文档根目录
            List<Student> list = new List<Student>();//创建对象集合         
            foreach (XmlNode stuNode in rootNode.ChildNodes)  //【4】遍历根节点(根节点包含所有节点)
            {
                if (stuNode.Name == "Student")
                {
                    Student objStu = new Student();
                    foreach (XmlNode subNode in stuNode)  //【5】遍历子节点
                    {
                        switch (subNode.Name)//根据子节点的名称封装到对象的属性
                        {
                            case "StuName":
                                objStu.StuName = subNode.InnerText;//获取《节点名称》对应的《节点值》
                                break;
                            case "StuAge":
                                objStu.StuAge = Convert.ToInt16(subNode.InnerText);
                                break;
                            case "Gender":
                                objStu.Gender = subNode.InnerText;
                                break;
                            case "ClassName":
                                objStu.ClassName = subNode.InnerText;
                                break;
                        }
                    }
                    list.Add(objStu);
                }
            }
            this.dgvStuList.DataSource = list;
        }
        //显示版本信息
        private void btnShowVersion_Click(object sender, EventArgs e)
        {
            //创建XML读取器
            XmlTextReader tReader = new XmlTextReader("StuScore.xml");

            string info = string.Empty;

            //循环查询
            while (tReader.Read())
            {
                if (tReader.Name == "Version")
                {
                    info = "版本:" + tReader.GetAttribute("vNo") + " 发布时间:" 
                        + tReader.GetAttribute("pTime");
                    break;
                }
            }
            MessageBox.Show(info,"数据版本");
        }
    }
}

 

XML文件操作

标签:drawing   obj   size   uname   com   alt   click   partial   getattr   

原文地址:https://www.cnblogs.com/funiyi816/p/14643228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!