0

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

        }
4

3 回答 3

0

利用

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

而不是简单

rtbox2.Text += DateTime.Now + "\n" + fi.FullName + " \nInstance: " + i.ToString() + "\n\n";
于 2012-10-08T06:20:19.680 回答
0

如果我理解正确,您只想在 i 大于 0 时显示文本?

if(i > 0 )
rtbox2.Text += DateTime.Now + "\n" + fi.FullName + " \nInstance: " + i.ToString() + "\n\n";
于 2012-10-08T06:20:54.160 回答
0

您总是可以在 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(),
        }));

在一行中完成所有操作(禁止格式)。:-)

于 2012-10-08T06:38:16.047 回答