Use Xpath is a querying tool -- it uses a language similar to SQL for
searching for specific conditions inside of Nodes.
If you are talking about searching all nodes for anything that
references a c keyword, then yes, your going to have to iterate through
the whole document, and take a look at the nodes that interest you, and
then do a regex, or string search for the keywords.
You could use XPath if say, you created an array of all the c# keywords
that you were interested in and then created an XPath query string to
search through them all. Here's sample code for using Xpath, it takes
the XML file and the Xpath string as arguments at the line command.
using System;
using System.Xml;
namespace {
class xpathtest2
{
[STAThread]
static void Main(string[] args)
{
if(args.Length < 2)
{
Console.WriteLine("Incorrect number of parameters");
goto endApp;
}
try
{
//Load the XML document
XmlDocument doc = new XmlDocument();
doc.Load(args[0]);
//Create XPathNavigator
XPathNavigator xpathNav = doc.CreateNavigator();
//Compile the XPath expression
XPathExpression xpathExpr = xpathNav.Compile(args[1]);
//Display the results depending on type of result
switch(xpathExpr.ReturnType)
{
case XpathResultType.Boolean:
Console.WriteLine("Boolean value: {0}",
xpathNav.Evaluate(xpathExpr));
break;
}
Chua Wen Ching wrote:
Hi everyone,
I am not that good in xml terms and coding in c#. To be honest.
ok, just imagine, i had this xml file.
<xml>
<language>C#</language>
<comments>cool language</comments>
<desc />
<author />
<company />
<codes />
</language>VB</language>
<comments>Never used before</comments>
<desc />
<author />
<company />
<codes />
...
...
</xml>
To search for C# keywords, i can just simple use XMLTextReader to do it.
There are few other terms, which i am so confused with.
XMLNode
XPath
XQuery
XInfer
or whatever X things? Maybe X-Men... hehe :)
I am looking for the best way to retrieve the fastest speed and best peformance when searching for a key inside the xml file.
I assume xml file is like a binary tree, BST. Correct me if i am wrong.
DTD and XSchema - i think this is more to define what is xml about? By adding a descriptive layer... hmm.. i guess so...
But again, just want to know the best way to search in a xml file which contains 10,000 <language /> tags. As i treat language as a unique key and used for searching.
No database used here. I think xml is a good way.