读取多个 XML 文件 [英] Reading Multiple XML files

查看:31
本文介绍了读取多个 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小型 XML 工具,用于查找多个 XML 文件中存在的元素数量.

I have Created a small XML tool, to find the numbers of element present in Multiple XML files.

此代码为 XML 文件中必须的元素提供了良好的结果.

This code gives the fine result for the elements which are must in XML files.

但是当涉及到 XML 文件中可能存在或不存在的特定元素时,软件给我的结果为:

But when it comes to specific elements, which may be present or not in XML files, Software give me result as:

10/8/2012 11:27:51 AM
C:\Documents and Settings\AlaspuMK\Desktop\KS\success\4CPK-PMF0-004D-P565-00000-00.xml 
Instance: 0

10/8/2012 11:27:51 AM
C:\Documents and Settings\AlaspuMK\Desktop\KS\success\4CPK-PMF0-004D-P566-00000-00.xml 
Instance: 0

10/8/2012 11:27:51 AM
C:\Documents and Settings\AlaspuMK\Desktop\KS\success\4CPK-PMF0-004D-P567-00000-00.xml 
Instance: 0

10/8/2012 11:27:51 AM
C:\Documents and Settings\AlaspuMK\Desktop\KS\success\4CPK-PMG0-004D-P001-00000-00.xml 
**Instance: 11**

10/8/2012 11:27:51 AM
C:\Documents and Settings\AlaspuMK\Desktop\KS\success\4CPK-PMG0-004D-P002-00000-00.xml 
Instance: 0

现在的问题是,当我搜索可能存在或不存在的标签时,XML 文件可能是 500-1000,该工具为我提供了每个文件的结果.在这种情况下,特定标签当前实例可能是 0 个或多个.

Now here the problem is XML files may be 500-1000 when i search the tag which may be present or not the tool gives me result for each and every files. In this case specific tag present instance may be 0 or multiple.

任何人都可以建议我的代码中的更改以查找实例大于0的文件名.如果实例> 0,则将其打印在文本框中.

Can any one suggest the changes in my Code to find the file name in which instance is greater than 0. and if instance > 0 print it in text box.

我当前的代码:

public void SearchMultipleTags()
        {
            if (txtSearchTag.Text != "")
            {
                try
                {
                    //string str = null;
                    //XmlNodeList nodelist;
                    string folderPath = textBox2.Text;
                    DirectoryInfo di = new DirectoryInfo(folderPath);
                    FileInfo[] rgFiles = di.GetFiles("*.xml");
                    foreach (FileInfo fi in rgFiles)
                    {
                        int i = 0;
                        XmlDocument xmldoc = new XmlDocument();
                        xmldoc.Load(fi.FullName);
                        //rtbox2.Text = fi.FullName.ToString();

                        foreach (XmlNode node in xmldoc.GetElementsByTagName(txtSearchTag.Text))
                        {

                            i = i + 1;

                            //
                        }
                        rtbox2.Text += DateTime.Now + "\n" + fi.FullName + " \nInstance: " + i.ToString() + "\n\n";
                        //rtbox2.Text += fi.FullName + "instances: " + str.ToString();
                    }

                }
                catch (Exception ex)
                {

                    MessageBox.Show("Invalid Path or Empty File name field.");


                }
            }
            else
            {
                MessageBox.Show("Dont leave field blanks.");
            }

        }

推荐答案

如果我没理解错,你想只在 i 大于 0 时才显示文本?

If I understand correctly, you want to display text only if the i is greater than 0?

if(i > 0 )
rtbox2.Text += DateTime.Now + "\n" + fi.FullName + " \nInstance: " + i.ToString() + "\n\n";

这篇关于读取多个 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆