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?
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.
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
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