如何使用关键字搜索现有的xml文档? [英] how to search existing xml document using keyword?

查看:141
本文介绍了如何使用关键字搜索现有的xml文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建XML文档搜索引擎....如果我给出一些关键字意味着引擎想要显示一些结果文档...我想在另一个窗口中以XML格式打开该XML文档...

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

推荐答案

您好Deenuji,



在XML文档中查找关键字(因为您似乎要求在标题中)



您可以使用 XPath 搜索XML或 Linq to XML。



使用XPath的示例文章

XPath读写器 [ ^ ]



另一种选择完全不同的选择是在原始文本中搜索模式,

最好使用 RegularExpressions 完成



请参阅以下示例代码:



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:

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);
    }
}





干杯,

Edo



Cheers,
Edo


添加以下方法



Add the following method

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;
        }





现在通过传递XML文档和节点名称来调用上面的方法。

还包括以下命名空间





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

using System.Xml;
using System.Xml.Schema;
using System.Xml.Linq;





希望这个会帮助你。

-Sijo



Hope this will help you.
-Sijo


这篇关于如何使用关键字搜索现有的xml文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆