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

本文详细介绍了如何使用C#通过 XmlDocument 和正则表达式在Winform应用中读取XML文件,并进行字符串的匹配。涉及实例代码和正则匹配的实现,适合开发者理解XML操作和在实际项目中的应用。
摘要由CSDN通过智能技术生成

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

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心海非海_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值