简体   繁体   English

读取多个XML文件

[英]Reading Multiple XML files

I have Created a small XML tool, to find the numbers of element present in Multiple XML files. 我创建了一个小的XML工具,以查找多个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: 但是,当涉及到XML文件中可能存在或不存在的特定元素时,软件给我的结果是:

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

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. 现在的问题是,当我搜索可能存在的标记时,XML文件的大小可能为500-1000,或者该工具没有为我提供每个文件的结果。 In this case specific tag present instance may be 0 or multiple. 在这种情况下,特定标签的当前实例可以为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. 谁能在我的代码中建议更改,以查找实例大于0的文件名。如果instance> 0,则在文本框中打印该文件名。

My current code: 我当前的代码:

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.");
            }

        }

Use 采用

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

instead of simple 而不是简单

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

If I understand correctly, you want to display text only if the i is greater than 0? 如果我理解正确,则只在i大于0时才显示文本?

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

You could always just use this code inside the try block: 您总是可以在try块内使用以下代码:

rtbox2.Text =
    String.Join(Environment.NewLine + Environment.NewLine,
        from fi in (new DirectoryInfo(textBox2.Text)).GetFiles("*.xml")
        let xd = XDocument.Load(fi.FullName)
        let i = xd.Descendants(txtSearchTag.Text).Count()
        where i > 0
        select String.Join(Environment.NewLine, new []
        {
            DateTime.Now.ToString(),
            fi.FullName,
            i.ToString(),
        }));

Does it all in one line (bar the formatting). 一行全部完成(排除格式)。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM