C# 在C中索引、搜索和存储信息的最佳方法#

C# 在C中索引、搜索和存储信息的最佳方法#,c#,xml,linq,C#,Xml,Linq,我正在写一个应用程序,它将需要索引和存储有关文件的信息快速。我目前正在使用XML存储信息,使用以下代码: XmlTextWriter xtw; xtw = new XmlTextWriter(FilePath, Encoding.UTF8); xtw.WriteStartDocument(); xtw.WriteStartElement("ApplicationIndex"); xtw.WriteEndElement

我正在写一个应用程序,它将需要索引和存储有关文件的信息快速。我目前正在使用XML存储信息,使用以下代码:

        XmlTextWriter xtw;
        xtw = new XmlTextWriter(FilePath, Encoding.UTF8);
        xtw.WriteStartDocument();
        xtw.WriteStartElement("ApplicationIndex");
        xtw.WriteEndElement();
        xtw.Close(); 

        XmlDocument xd = new XmlDocument();
        FileStream lfile = new FileStream(FilePath, FileMode.Open);
        xd.Load(lfile);
        XmlElement cl = xd.CreateElement("Application");
        cl.SetAttribute("Name", ApplicationName);
        XmlElement na = xd.CreateElement("Path");
        XmlText natext = xd.CreateTextNode(ApplicationPath);
        na.AppendChild(natext);
        cl.AppendChild(na);
        XmlElement na1 = xd.CreateElement("UseCount");
        XmlText natext1 = xd.CreateTextNode("0");
        na1.AppendChild(natext1);
        cl.AppendChild(na1);
        XmlElement na2 = xd.CreateElement("SearchTerm");
        XmlText natext2 = xd.CreateTextNode(ApplicationName.ToLower());
        na2.AppendChild(natext2);
        cl.AppendChild(na2);
        xd.DocumentElement.AppendChild(cl);
        lfile.Close();
        xd.Save(FilePath);
        listBox1.Items.Clear();
        var doc = XDocument.Load(filePath);
        foreach (var child in doc.Descendants("SearchTerm"))
        {
            if (child.Value.Contains(textBox1.Text.ToLower()))
            {
                listBox1.Items.Add(child.Value);
            }
        }
这对于创建文件和存储数据来说效果很好,但是由于文档中有几百个节点,所以我很难快速搜索数据。我已经尝试使用Linq to XML通过以下代码实现这一点:

        XmlTextWriter xtw;
        xtw = new XmlTextWriter(FilePath, Encoding.UTF8);
        xtw.WriteStartDocument();
        xtw.WriteStartElement("ApplicationIndex");
        xtw.WriteEndElement();
        xtw.Close(); 

        XmlDocument xd = new XmlDocument();
        FileStream lfile = new FileStream(FilePath, FileMode.Open);
        xd.Load(lfile);
        XmlElement cl = xd.CreateElement("Application");
        cl.SetAttribute("Name", ApplicationName);
        XmlElement na = xd.CreateElement("Path");
        XmlText natext = xd.CreateTextNode(ApplicationPath);
        na.AppendChild(natext);
        cl.AppendChild(na);
        XmlElement na1 = xd.CreateElement("UseCount");
        XmlText natext1 = xd.CreateTextNode("0");
        na1.AppendChild(natext1);
        cl.AppendChild(na1);
        XmlElement na2 = xd.CreateElement("SearchTerm");
        XmlText natext2 = xd.CreateTextNode(ApplicationName.ToLower());
        na2.AppendChild(natext2);
        cl.AppendChild(na2);
        xd.DocumentElement.AppendChild(cl);
        lfile.Close();
        xd.Save(FilePath);
        listBox1.Items.Clear();
        var doc = XDocument.Load(filePath);
        foreach (var child in doc.Descendants("SearchTerm"))
        {
            if (child.Value.Contains(textBox1.Text.ToLower()))
            {
                listBox1.Items.Add(child.Value);
            }
        }
这是非常快,但我似乎无法获得任何有关选定节点的信息。例如,我想根据UseCount对返回的结果进行排序(计数越高,列表越高)。是否有任何方法可以在XML或任何其他技术中实现这一点

这是XML文件的外观:

    <?xml version="1.0" encoding="utf-8"?>
<ApplicationIndex>
  <Application Name="Google Chrome">
    <Path>C:\Program Files\Google\Chrome\Chrome.exe</Path>
    <UseCount>0</UseCount>
    <SearchTerm>google chrome</SearchTerm>
  </Application>
  <Application Name="Mozilla Firefox">
    <Path>C:\Program Files\Mozilla\Firefox\Firefox.exe</Path>
    <UseCount>0</UseCount>
    <SearchTerm>mozilla firefox</SearchTerm>
  </Application>
</ApplicationIndex>

C:\ProgramFiles\Google\Chrome\Chrome.exe
0
谷歌浏览器
C:\ProgramFiles\Mozilla\Firefox\Firefox.exe
0
mozilla firefox

您可以按
UseCount
按降序对元素进行排序,如下所示:

var doc = XDocument.Load(filePath);
var elements = doc.Descendants("Application")
               .OrderByDescending(x => (int)x.Element("UseCount"));
要通过给定的
SearchTerm
搜索记录,可以执行以下操作:

var element = doc.Descendants("Application")
             .FirstOrDefault(x => (string)x.Element("SearchTerm") == value);

if(element != null)
{
    // record found
}

您能显示xml结构吗?谢谢,这很有效,但搜索位要求我键入整个短语。是否有方法搜索该值是否包含搜索项?出现此错误:错误1“System.Xml.Linq.XElement”不包含“contains”的定义,并且最佳扩展方法重载“System.Linq.ParallelEnumerable.contains(System.Linq.ParallelQuery,TSource)”具有一些无效属性arguments@KieranCrown您可以尝试:
((字符串)x、 元素(“SearchTerm”))。包含(值)
@KieranCrown使用
Where
而不是
FirstOrDefault
这是我得到的当前代码:'listBox1.Items.Clear();var doc=XDocument.Load(文件路径);var element=doc.subjects(“应用程序”)。其中(x=>((字符串)x.element(“搜索术语”))。包含(textBox1.Text));foreach(元素中的XElement w){listBox1.Items.Add(w.element(“Path”).Value);}'它可以工作,但不按UseCount排序?