-1

am developing a search application in ASP.NET. I am storing the data in an XML file.when i search for data in textbox and click submit button it should search complete XML file and retrieve the data. this is the sample xml data...

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

now when i search in this form "details of vamshi" in textbox it should display the vamshi details.how can i do this..

sinsuren
  • 1,745
  • 2
  • 23
  • 26
naveenkumar
  • 23
  • 1
  • 11
  • Try using classic DOM object XmlDocument with xpath or XDocument from .net 4.0. As per msdn it is a new version of XmlDocument and bit faster than old one. Also Xdocument is much simpler to use. http://stackoverflow.com/questions/1542073/xdocument-or-xmldocument – 0cool Aug 02 '16 at 14:36

3 Answers3

0

You can use a custom text searching process to search the xml as if it were a text file. But, I wouldn't recommend this.

The best solution in my opinion is to dump the xml file into a class college in your program.

To accomplish this:

  1. Setup the college class and the student class to match the xml file. E.g. college should have a List<student> and student should have an id and a name. Probably both strings since you have leading zeros.
  2. Download and add this XML helper class to your project.

  3. dump your college xml into the class you created:

    College c = new College();        
    c = XmlHelper.FromXmlFile<College>(@"/path/to/XML/File");
    
  4. Your College class is now populated with the data from the xml, and you can perform the search operations you desire.

Hope this helps!

Kikootwo
  • 360
  • 2
  • 14
0

In order to achieve what you are looking for, first of all, you need to parse the xml files into C# objects. By doing that, you load xml file into memory.

Then using the built in C# xml parser function to trace the xml node by your given piece of information, in your case is "Name" attribute of student object.

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

I recently wrote something up that might help you and it is written in 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;
        }

This should solve your problem or anyone else's for that matter.