locked
How to search for a pattern in multiple xml files and return the list of file names. RRS feed

  • Question

  • HI Folks,
    I am newbie to LINQ.

    Suppose I have a mutliple xml files,which contains employee info(Name,contact number and so on).
    How do I search my contact number(suppose it is 987652345,which may be in mutliple xmle files) in these xml files and return back the list of file names which contains my conatct number 987652345 suing LINQ to XML in C#


    Thanks in Advance
    Wednesday, March 17, 2010 10:02 AM

Answers

  • Hi soni_qureshi,

    You can loop through these xml files and find the contact number one by one, if the contact number is been found, then add the file names to a list of string. Please refer to the following sample code:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        List<string> fileNameList = new List<string>();
        private void button1_Click(object sender, EventArgs e)
        {
            //string url = Server.MapPath(Data.xml);
            string[] fileNames = Directory.GetFiles("D:\\");
    
            foreach (string xmlFile in fileNames)
            {
                XElement xes = XElement.Load(xmlFile);
    
                IEnumerable<XElement> elements = from ee in xes.Descendants("contact number")
                                                 where ee.Value == "987652345"
                                                 select ee;
    
                if (elements.Count() > 0)
                {
                    fileNameList.Add(xmlFile);
                } 
            }
        }
    }
    

     

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, March 19, 2010 4:42 AM
    Moderator

All replies

  • Hi soni_qureshi,

    You can loop through these xml files and find the contact number one by one, if the contact number is been found, then add the file names to a list of string. Please refer to the following sample code:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        List<string> fileNameList = new List<string>();
        private void button1_Click(object sender, EventArgs e)
        {
            //string url = Server.MapPath(Data.xml);
            string[] fileNames = Directory.GetFiles("D:\\");
    
            foreach (string xmlFile in fileNames)
            {
                XElement xes = XElement.Load(xmlFile);
    
                IEnumerable<XElement> elements = from ee in xes.Descendants("contact number")
                                                 where ee.Value == "987652345"
                                                 select ee;
    
                if (elements.Count() > 0)
                {
                    fileNameList.Add(xmlFile);
                } 
            }
        }
    }
    

     

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, March 19, 2010 4:42 AM
    Moderator
  • I think this works fine for me...I will check and let you know..Thanks a lot for your time.I really appreciate your help
    Monday, March 22, 2010 4:30 AM