资源简介

学习读取XML显示在dataGridView

资源截图

代码片段和文件信息

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 WindowsFormsApp1
{
    public partial class MainWindow : Form
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender EventArgs e)
        {

        }

        private void label2_Click(object sender EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();//一个打开文件的对话框
            string xml_FilePath = ““;//用来记录当前打开文件的路径的
            openFileDialog1.Filter = “xml文件(*.xml)|*.xml“;//设置允许打开的扩展名
            if (openFileDialog1.ShowDialog() == DialogResult.OK)//判断是否选择了文件  
            {
                xml_FilePath = openFileDialog1.FileName;//记录用户选择的文件路径
                xmlDocument xmlDocument = new xmlDocument();//新建一个xml“编辑器”
                xmlDocument.Load(xml_FilePath);//载入路径这个xml
                try
                {
                    xmlNodeList xmlNodeList = xmlDocument.SelectSingleNode(“malldata“).ChildNodes;//选择malldata为根结点并得到旗下所有子节点
                    dataGridView1.Rows.Clear();//清空dataGridView1,防止和上次处理的数据混乱
                    foreach (xmlNode xmlNode in xmlNodeList)//遍历malldata的所有节点
                    {
                        xmlNode list1 = xmlNode.SelectSingleNode(“//item_list[item]“);
                        xmlNode list2 = xmlNode.SelectSingleNode(“//mall_tree[catalog]“);
                        xmlNode list3 = xmlNode.SelectSingleNode(“//service_list[service]“);
                        xmlNodeList xml1 = list1.ChildNodes;
                        xmlNodeList xml2 = list2.ChildNodes;
                        xmlNodeList xml3 = list3.ChildNodes;
                        foreach (xmlNode xnode in xml1)
                        {
                            xmlElement xml = (xmlElement)xnode;

                            xmlNodeList xe1s1 = xml.GetElementsByTagName(“name“);
                            xmlNodeList xe1s2 = xml.GetElementsByTagName(“templ_id“);
                            xmlNodeList xe1s3 = xml.GetElementsByTagName(“count“);
                            xmlNodeList xe1s4 = xml.GetElementsByTagName(“time_limit“);
                            xmlNodeList xe1s5 = xml.GetElementsByTagName(“is_new“);
                            xmlNodeList xe1s6 = xml.GetElementsByTagName(“is_hot“);
                            xmlNodeList xe1s7 = xml.GetElementsByTagName(“original_price“);
                            xmlNodeList xe1s8 = xml.GetElementsByTagName(“restrict_level_min“);
                            xmlNodeList xe1s9 =

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件       23502  2015-11-16 11:38  pgp_23.ico
     目录           0  2020-08-17 11:31  WindowsFormsApp1\
     文件        1135  2020-08-16 00:15  WindowsFormsApp1.sln
     目录           0  2020-08-16 01:52  WindowsFormsApp1\bin\
     目录           0  2020-08-17 11:33  WindowsFormsApp1\bin\Debug\
     文件      870442  2020-08-17 02:45  WindowsFormsApp1\bin\Debug\malldata.xml
     文件       58368  2020-08-17 11:30  WindowsFormsApp1\bin\Debug\WindowsFormsApp1.exe
     文件       56832  2020-08-17 11:30  WindowsFormsApp1\bin\Debug\WindowsFormsApp1.pdb
     目录           0  2020-08-16 01:52  WindowsFormsApp1\bin\Release\
     文件       16970  2020-08-17 11:31  WindowsFormsApp1\MainWindow.cs
     文件       55697  2020-08-17 11:31  WindowsFormsApp1\MainWindow.Designer.cs
     文件       45837  2020-08-17 11:31  WindowsFormsApp1\MainWindow.resx
     文件        3415  2020-08-17 05:42  WindowsFormsApp1\MallEditor.csproj
     目录           0  2020-08-16 00:09  WindowsFormsApp1\obj\
     目录           0  2020-08-17 11:28  WindowsFormsApp1\obj\Debug\
     文件        1209  2020-08-16 02:23  WindowsFormsApp1\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6888  2020-08-17 05:42  WindowsFormsApp1\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件          42  2020-08-17 05:42  WindowsFormsApp1\obj\Debug\MallEditor.csproj.CoreCompileInputs.cache
     文件         852  2020-08-17 10:59  WindowsFormsApp1\obj\Debug\MallEditor.csproj.FileListAbsolute.txt
     文件         910  2020-08-17 11:28  WindowsFormsApp1\obj\Debug\MallEditor.csproj.GenerateResource.cache
     文件        9219  2020-08-16 01:52  WindowsFormsApp1\obj\Debug\MallEditor.csprojAssemblyReference.cache
     目录           0  2020-08-16 00:09  WindowsFormsApp1\obj\Debug\TempPE\
     文件          42  2020-08-16 00:09  WindowsFormsApp1\obj\Debug\WindowsFormsApp1.csproj.CoreCompileInputs.cache
     文件         770  2020-08-16 00:11  WindowsFormsApp1\obj\Debug\WindowsFormsApp1.csproj.FileListAbsolute.txt
     文件         789  2020-08-16 00:11  WindowsFormsApp1\obj\Debug\WindowsFormsApp1.csproj.GenerateResource.cache
     文件        9219  2020-08-16 00:09  WindowsFormsApp1\obj\Debug\WindowsFormsApp1.csprojAssemblyReference.cache
     文件       58368  2020-08-17 11:30  WindowsFormsApp1\obj\Debug\WindowsFormsApp1.exe
     文件       24068  2020-08-17 11:28  WindowsFormsApp1\obj\Debug\WindowsFormsApp1.MainWindow.resources
     文件       56832  2020-08-17 11:30  WindowsFormsApp1\obj\Debug\WindowsFormsApp1.pdb
     文件         180  2020-08-17 05:42  WindowsFormsApp1\obj\Debug\WindowsFormsApp1.Properties.Resources.resources
     文件         502  2020-08-17 02:17  WindowsFormsApp1\Program.cs
............此处省略6个文件信息

评论

共有 条评论