C# 读取 xml 文件 | 匹配正则表达式 | 输出结果 | 最终结合Winform 开发出一个桌面应用程序(全过程详解,含源码)

C# 读取 xml 文件并进行匹配

1. 读取

1.1 读取效果图

在这里插入图片描述

1.2 读取代码

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

namespace ConsoleApp1
{
    
    
    public class RWXml
    {
    
    
        // 读取 xml 文档
        public void ReadXml()
        {
    
    
            // 实例化一个 xml 操作对象
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load("C:/Users/Z004E3MK/source/repos/第一个窗体应用程序/ConsoleApp1/001copy.xml"); 

            // 获取根节点 Configurations
            XmlNode node = xDoc.SelectSingleNode("Configurations");
            XmlNodeList nodeList = node.ChildNodes;

            foreach (XmlNode xn in nodeList)
            {
    
    
                /*
                string name = xn.Name;
                Console.WriteLine(name);
               */

                // 类型强制转换
                XmlElement xmle = (XmlElement)xn;
                string senser = xmle.GetAttribute("Senser");
                Console.WriteLine(senser);
            }
            Console.ReadKey();

        }
    }
}

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

namespace ConsoleApp1
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            RWXml xml = new RWXml();
            xml.ReadXml();
  
        }
    }
}

https://www.bilibili.com/video/BV1As411g7od?from=search&seid=16539484488326837186&spm_id_from=333.337.0.0

2. 正则匹配

2.1 匹配效果图

在这里插入图片描述

2.2 匹配代码(保留修改痕迹版)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Collections;
using System.Text.RegularExpressions;


namespace XML
{
    
    
    class Program
    {
    
    
        // 配置表中各个节点的信息
        private class NodeInfo
        {
    
    
            //Name
         //**原   public String Name { get; set; }
            public String RegExp {
    
     get; set; }

            //Path
            public String Path {
    
     get; set; }
            //**   public String Path { get; set; }

            //Subpath
            //**   public String Subpath { get; set; }

            //Iseff
            public String Iseff {
    
     get; set; }
        }

        //配置表信息列表
        private List<NodeInfo> m_nodeInfoList = new List<NodeInfo>();
        //** private string m_xmlFilePath = "\\输出路径配置表.xml";
        private string m_xmlFilePath = "C:/Users/Z004E3MK/source/repos/第一个窗体应用程序/ConsoleApp1/all.xml";
        private bool m_isEff = false;

        //获取配置表中信息
        private void ReadExporterConfigurationInfoFromFile()
        {
    
    
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(this.m_xmlFilePath);
            this.m_nodeInfoList.Clear();
            //** foreach (XmlNode node in xmlDoc.SelectSingleNode("rules").SelectNodes("rule"))
            foreach (XmlNode node in xmlDoc.SelectSingleNode("Configurations").SelectNodes("RegExpPattern"))
            {
    
    
                NodeInfo newNode = new NodeInfo();

                //**   newNode.Name = node.Attributes["name"].Value;
                newNode.RegExp = node.Attributes["regExp"].Value;
                //**   newNode.Path = node.Attributes["path"].Value;
                //**   newNode.Subpath = node.Attributes["subpath"].Value;
                newNode.Iseff = node.Attributes["iseff"].Value;

                this.m_nodeInfoList.Add(newNode);
            }
        }

        //进行匹配
        private bool CanMatch(string str) 
        {
    
    
            string RegexStr = string.Empty;
            foreach (NodeInfo n in m_nodeInfoList)  //匹配路径
            {
    
    
              //**  RegexStr = n.Name;
                RegexStr = n.RegExp;

                if (Regex.IsMatch(str, RegexStr))
                {
    
    
                    this.m_isEff = n.Iseff == "true" ? true : false;
                    return true;
                }
            }
            return false;
        }

        static void Main(string[] args)
        {
    
    
            string str = "c5";   // 需要进行匹配的字符串
            while (true)
            {
    
    
                Program p = new Program();
                //获取配置表信息
                p.ReadExporterConfigurationInfoFromFile();
                // 匹配字符串
                bool isMatch = p.CanMatch(str);
                if (isMatch)
                {
    
    
                    Console.Write("匹配成功!");
                }
                else
                {
    
    
                    Console.Write("匹配失败!");
                }
                   str = Console.ReadLine();
            }

        }
    }
}

参考博客

读取XML并通过正则表达式进行匹配
.NET[C#]中NullReferenceException(未将对象引用到实例)是什么问题?如何修复处理?

3. 结合 Winform

猜你喜欢

转载自blog.csdn.net/weixin_46644403/article/details/120995131