Results 1 to 7 of 7

Thread: What is the best way for search a string value in an XML file

  1. #1

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    What is the best way for search a string value in an XML file

    Hi all, I have the following problem :

    I have a list of XML files that contain data for an application. I need to loop through each of those files to find a search string. If found I need to store each matching node, together with its parents, into a treeview. Any ideas how this can be achieved?
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  2. #2
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: What is the best way for search a string value in an XML file

    can you show the format of yor xml?

  3. #3
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: What is the best way for search a string value in an XML file

    See this article: XML TreeView
    Shows a way to work with Xml files using the XmlDocument class.

  4. #4

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Re: What is the best way for search a string value in an XML file

    Actually I can load an XML file into a treeview, the tricky part actually if finding the search string into the files and then load these find matched into a treeview. Please find attached an example of my xml file.
    Attached Files Attached Files
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  5. #5
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: What is the best way for search a string value in an XML file

    There's a language for searching through XML files. I think it's called XQL. It's supported in .NET. I recommend you to google for it. I'm sure you'll find something useful.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  6. #6
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: What is the best way for search a string value in an XML file

    Sorry, I took a look at your file and it is rather easy filestructure so maybe it is easier just doing it with an XmlDocument yes.

    I'll post you some code:

    Code:
     
    
                List<XmlNode> list = new List<XmlNode>();
                XmlDocument doc = new XmlDocument();
                doc.Load(fullFileName);
                searchKeyWord = "blablabla.txt";
                MakeList(list, (XmlElement)(doc.ChildNodes[0]), searchKeyWord);
    
    
    
           public void MakeList(List<XmlNode> list, XmlElement element, string searchKeyWord)
            {
                foreach (XmlElement node in element.ChildNodes)
                 {
                         if (node.Name.ToLower() == "node" && node.Attributes["text"].Value ==searchKeyWord)
                         {
                                  list.Add(node);
                         }
                         if (node.ChildNodes.Count>0){
                                  MakeList(list, node, searchKeyWord)
                         }
                 }
                 return list;
            }


    this should do the trick. Here I just looked if the nodename was "node" and the checked if the attribute was equal to a certain keyword. Of course you could use something else like Contains(searchKeyWord) or StartsWith(searchKeyWord), ...

    It is a recursive method. Which means that it calls itself to search into childnodes of a childnodes of a childnodes of a childnodes of a childnodes of a node

    There are 3 things to note.

    Note 1
    I wrote this code for .NET 2005 with the following using reference.
    using System.Collections.Generic;

    You will need to place this on top of your project file.

    This is necessary for the List<XmlNode> If you do not know what it is and it gives errors, then you just replace every List<XMLNode> by a usual ArrayList. It will also work.

    Note 2
    This rule:
    doc.ChildNodes[0]
    I used it assuming you have a kind of root element in you XMLfile which contains all data. If it doesn't work try just doc.ChildNodes instead.

    Note 3
    I just wrote it here without testing it or checking the syntax. So there could be stupid mistakes in it. Don't hesitate to ask

    Let me know if it worked

    Greetings
    BramGo
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: What is the best way for search a string value in an XML file

    It's XPath you want.

    Load the XML into an XMLDocument, then use the .SelectNodes() method, passing it an xquery:

    /firstnode/secondnode[contains(somenode/text(),"thetextyouwant")]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width