0

I have created a small XML tool which gives me count of specific XML tags from multiple XML files.

The code for this is as follow:

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;

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

                        }
                        else 
                        {
                            //MessageBox.Show("No Markup Found.");
                        }

                        //rtbox2.Text += fi.FullName + "instances: " + str.ToString();
                    }

                }
                catch (Exception)
                {

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


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

        }

This code returns me the tag counts in Multiple XML files which user wants.

Now the same I want to Search for particular text and its count present in XML files.

Can you suggest the code using XML classes.

Thanks and Regards, Mayur Alaspure

Mayur Alaspure
  • 69
  • 1
  • 2
  • 11
  • : what did you try Mayur ? Your previous post http://stackoverflow.com/questions/12776198/reading-multiple-xml-files also had very same code snippet. What did you try before asking question. – Milind Thakkar Oct 10 '12 at 05:27
  • @MilindThakkar: That one was for the Specific Markup and now i am looking for specific text in XML file. And Thanks for your previous answer it works and i am using your code only. – Mayur Alaspure Oct 10 '12 at 05:32
  • Two points Mayur: One, I can't see any change in the code snippet given in the other question and this one. So doubtful about what you tried to do before asking question here. Second, if you are happy with Anyone's answer, you should mark that as "answer" and/or "upvote".-Milind – Milind Thakkar Oct 10 '12 at 05:52
  • I am New So not so reputations to upvote sorry for that. – Mayur Alaspure Oct 10 '12 at 06:00

3 Answers3

0

System.Xml.XPath.

Xpath supports counting: count(//nodeName)

If you want to count nodes with specific text, try

count(//*[text()='Hello'])

See How to get count number of SelectedNode with XPath in C#?

By the way, your function should probably look something more like this:

private int SearchMultipleTags(string searchTerm, string folderPath) { ...
      //...
      return i;
}
Community
  • 1
  • 1
Jeffrey Knight
  • 5,888
  • 7
  • 39
  • 49
0

Use LINQ2XML instead..It's simple and a complete replacement to othe XML API's

XElement doc = XElement.Load(fi.FullName);

//count of specific XML tags
int XmlTagCount=doc.Descendants().Elements(txtSearchTag.Text).Count();

//count particular text

int particularTextCount=doc.Descendants().Elements().Where(x=>x.Value=="text2search").Count();
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • Thanks a lot your code gives me the count of particular tag in XML file. but i want to search a particular text in XML files. – Mayur Alaspure Oct 10 '12 at 05:34
  • @MayurAlaspure check out the second part of the code in the ans – Anirudha Oct 10 '12 at 05:35
  • I am facing one more problem. I am creating applications on framework 3.5 and running on development machine but when I run that application on client's PC some of the class like lineNumber, Line Position are not working. Is this framework issue or what? – Mayur Alaspure Oct 10 '12 at 05:45
0

Try using XPath:

//var document = new XmlDocument();
int count = 0;
var nodes = document.SelectNodes(String.Format(@"//*[text()='{0}']", searchTxt));
if (nodes != null)
    count = nodes.Count;
Ria
  • 10,237
  • 3
  • 33
  • 60