Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
i want to create XML document search engine....if i give some keyword means that engine want to show some of results documents...thn i want to open that XML document in XML format in another window...
Posted

Hi Deenuji,

To find a keyword in the XML document (as it seems you requested in the title)

You can either use XPath to search the XML or Linq to XML.

An example article for using XPath
XPath reader-writer[^]

The other option radically different in thinking is to search the raw text for the pattern,
that will best be accomplished using RegularExpressions

See following example code:

C#
public static void FindXMLs(FolderPath)
{
    string [] files = Directory.GetFiles(FolderPath);

    foreach(string fileName in fileEntries)
    {
        XDocument xdoc = XDocument.Load(fileName);
        
        // Now here's the part where you do whatever you want with the element
        // You can check if it exists\contains the f
        var element = xdoc.Elements("YourNode2Find_ComesHere").Single();
        element.Value = "foo"; // Do something with it
        xdoc.Save(fileName);
    }
}


Cheers,
Edo
 
Share this answer
 
v5
Comments
Deenuji 21-Jan-13 5:57am    
s sir i need example project....actually i want to insert keyword on XML files ....based on this keyword i need to show my XML document as results....
Joezer BH 21-Jan-13 6:02am    
Added a link to the solution for using XPath,
if you want specific examples or link for using Linq or RegExp, lemme know :)
Deenuji 21-Jan-13 6:07am    
i need based on keyword xml document search project sir!!!!
Deenuji 21-Jan-13 6:26am    
all this projects r based on single XML document....i need to search form existing multiple xml documents
Joezer BH 21-Jan-13 7:03am    
I've added a search through a folder, see above.
Of course you can vary the input to be a string array of file paths of your wish etc.
Add the following method

C#
public static string GetElementByNodeName(XmlDocument xmldocInput, string NodeName)
        {
            string Output = string.Empty;
            try
            {

                if (xmldocInput.GetElementsByTagName(NodeName).Count > 0)
                {
                    Output = xmldocInput.GetElementsByTagName(NodeName).Item(0).InnerXml;
                }//if
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return Output;
        }


Now call the above method by passing the XML document and node name.
Also include the following namespaces

C#
using System.Xml;
using System.Xml.Schema;
using System.Xml.Linq;


Hope this will help you.
-Sijo
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900