Reading Multiple XML files

2019-09-02 09:22发布

问题:

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

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

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

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.

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.

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

        }

回答1:

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";


回答2:

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";


回答3:

You could always just use this code inside the try block:

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). :-)