XML——读取文件夹下所有的Xml文件,并修改属性,保留上次打开文件的位置

功能:读取文件夹下所有的Xml文件,并修改属性,保留上次打开文件的位置

using System;

using System.Windows.Forms;

using System.IO;using System.Xml;

using Microsoft.Win32;

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        { InitializeComponent();  }

        private void button1_Click(object sender, EventArgs e)

        {

            FolderBrowserDialog fb = new FolderBrowserDialog();

            RegistryKey myReg1, myReg2; myReg1 = Registry.CurrentUser;

            try

            {

                myReg2 = myReg1.OpenSubKey("Software\\MySoft");

                fb.SelectedPath = myReg2.GetValue("1").ToString();

            }

            catch { }

            if (fb.ShowDialog() == DialogResult.OK)

            { SearchDir(fb.SelectedPath); }

            myReg2 = myReg1.CreateSubKey("Software\\MySoft");

            myReg2.SetValue("1", fb.SelectedPath);

        }

        private void SearchDir(string fileName)

        {

            DirectoryInfo dirInfo = new DirectoryInfo(fileName);

            if (dirInfo.Exists == false){ return; }

            FileSystemInfo[] files = dirInfo.GetFileSystemInfos();

            foreach (FileSystemInfo file in files)

            {

                if (file.Name == ".svn") { continue; }

                FileInfo fileInfo = file as FileInfo;

                if (fileInfo == null){ SearchDir(file.FullName); }//null说明是文件夹

                else  { Update(file.FullName); }

            }

        }

        private void Update(string fileName)

        {

            XmlDocument doc = new XmlDocument();

            doc.Load(fileName);

            XmlNodeList nodeP = doc.SelectNodes("MeasureItem/Params/Param");

            if (nodeP == null || nodeP.Count == 0)

            { nodeP = doc.SelectNodes("ScriptInfo/Param/Param"); }

            foreach (XmlNode node in nodeP)

            {

                XmlElement xleP = (XmlElement)node;

                if (xleP.ChildNodes.Count > 0)

                {

                    XmlNodeList nodeList = node.ChildNodes;

                    for(int i=0;i<nodeList.Count;i++)

                    {

                        if (nodeList[i] is XmlElement)

                        {

                            XmlElement eleNode = (XmlElement)nodeList[i];

                            if (eleNode.Name == "Rows" || eleNode.Name == "DefaultRows")

                            {

                                XmlNodeList nodeChildren = eleNode.ChildNodes;

                                for (int j = 0; j < nodeChildren.Count; j++)

                                {

                                    if (nodeChildren[j] is XmlElement)

                                    {

                                        XmlElement eleChild = (XmlElement)nodeChildren[j];

                                        eleChild.SetAttribute("id", (j + 1).ToString());

                                        eleChild.SetAttribute("order", (j + 1).ToString());

                                    }

                                }

                            }

                        }

                    }

                }

                doc.Save(fileName);

            }

        }

    }

}

发布了60 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/105593838
今日推荐