【问题标题】:how to search the data from XML file?如何从 XML 文件中搜索数据?
【发布时间】:2016-08-02 11:31:20
【问题描述】:

正在 ASP.NET 中开发搜索应用程序。我将数据存储在 XML file 中。当我在文本框中搜索数据并单击提交按钮时,它应该搜索完整的 XML 文件并检索数据。 这是示例 xml 数据...

<college>
    <students>
        <student>
            <name>harish</name>
            <id>002</id>
        </student>
        <student>
            <name>vamshi</name>
            <id>003</id>
        </student>
    </students>
</college>

现在,当我在文本框中以这种形式“vamshi 的详细信息”搜索时,它应该显示 vamshi 的详细信息。我该怎么做..

【问题讨论】:

标签: c# asp.net xml


【解决方案1】:

为了实现你想要的,首先,你需要将xml文件解析成C#对象。通过这样做,您将 xml 文件加载到内存中。

然后使用内置的 C# xml 解析器函数通过您给定的信息来跟踪 xml 节点,在您的情况下是学生对象的“名称”属性。

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Path\To\Xml\File.xml");
XmlNode node = doc.SelectSingleNode("//Student/Name/");
// node.Value contains "test@ABC.com"

【讨论】:

    【解决方案2】:

    您可以使用自定义文本搜索过程来搜索 xml,就好像它是一个文本文件一样。但是,我不会推荐这个。

    我认为最好的解决方案是将 xml 文件转储到程序中的 college 类中。

    要做到这一点:

    1. 设置college 类和student 类以匹配xml 文件。例如。大学应该有一个List&lt;student&gt;,学生应该有一个id和一个name。可能是两个字符串,因为你有前导零。
    2. Download and add this XML helper class to your project.

    3. 将你的大学 xml 转储到你创建的类中:

      College c = new College();        
      c = XmlHelper.FromXmlFile<College>(@"/path/to/XML/File");
      
    4. 您的 College 课程现在已使用 xml 中的数据填充,您可以执行所需的搜索操作。

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      我最近写了一些可能对你有帮助的东西,它是用 C# 编写的:

              public string GetConnectionString(string nodeName, string xmlFile)
              {
                  XmlDocument xmLDocument = new XmlDocument();
                  xmLDocument.Load(xmlFile);
                  XmlElement root = xmLDocument.DocumentElement;
                  foreach (XmlNode childNode in root.ChildNodes) 
                  {
                      return RecurseNodeTree(childNode, nodeName);
                  }
                  return null;
              }
      
              private string RecurseNodeTree(XmlNode xmlNode, string nodeName) 
              {
                  if (xmlNode.Name == nodeName) 
                  {
                      return xmlNode.InnerText;
                  }
      
                  if (xmlNode.HasChildNodes) 
                  {
                      foreach (XmlNode childNode in xmlNode.ChildNodes) 
                      {
                          if (xmlNode.NextSibling != null)
                          {
                              return RecurseNodeTree(xmlNode.NextSibling, nodeName);
                          }
      
                          return RecurseNodeTree(childNode, nodeName);
                      }
                  }
      
                  if (xmlNode.NextSibling != null)
                  {
                      return RecurseNodeTree(xmlNode.NextSibling, nodeName);
                  }
      
                  return null;
              }
      

      这应该可以解决您或其他任何人的问题。

      【讨论】:

        猜你喜欢
        • 2011-01-20
        • 2023-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多