I need to search an XML file for a unique entry. I've never worked with XML before. Can anyone tell me what the fastest way to search an XML file is? For the website I'm working on performance is critical so I need that fastest possible way to get this data. For this particular search, all I need is to get the text inside of <TERM> based on the string id ("entry_id" in the XML file).
Example:

string getTermName(string id);

label.text = getTermName("0001");

Here is a sample of how the XML file is structured.

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE GLOSSARIES SYSTEM "glossary.dtd">
<GLOSSARIES>
  <GLOSSARY language="English">
    <ENTRY entry_id="0001">
      <TERM>Term Name</TERM>
      <DEFINITION>
        A text definition of Term Name.
      </DEFINITION>
      <TOOL>PlanFinder</TOOL>
    </ENTRY>
  </GLOSSARY>
</GLOSSARIES>

Recommended Answers

All 3 Replies

Load your xml file in DataTable with the help of ReadXml() method.. Then user DataTable.Select("Entry_Id = '0001'");. The method will return array of datarow and with the help of your datarow array you will get the value of Term for given entry id..

I need to search an XML file for a unique entry. I've never worked with XML before. Can anyone tell me what the fastest way to search an XML file is? For the website I'm working on performance is critical so I need that fastest possible way to get this data. For this particular search, all I need is to get the text inside of <TERM> based on the string id ("entry_id" in the XML file).
Example:

string getTermName(string id);

label.text = getTermName("0001");

Here is a sample of how the XML file is structured.

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE GLOSSARIES SYSTEM "glossary.dtd">
<GLOSSARIES>
  <GLOSSARY language="English">
    <ENTRY entry_id="0001">
      <TERM>Term Name</TERM>
      <DEFINITION>
        A text definition of Term Name.
      </DEFINITION>
      <TOOL>PlanFinder</TOOL>
    </ENTRY>
  </GLOSSARY>
</GLOSSARIES>

using XmlReader is the fastest way to read data from the xml file then use XPath to traverse to the node of your data

Thanks for the responses. I actually got something working before you guys responded. If the performance testers don't throw a fit about it I'll probably leave it for now, but if they do I'll definitely try your suggestions out. I went with this

public static string getGlossaryTermName(string id)
        {
            string termName = "";
            XmlDocument doc = new XmlDocument();
            XmlNodeList nodelist;
            doc.XmlResolver = null;
            
            if (PlanFinderSessionManager.Demographics.LanguageIndex == 2)
            {
                doc.Load(ServerUtilities.getSiteName() + pathName);
            }
            else //default to English
            {
                doc.Load(ServerUtilities.getSiteName() + pathName);
            }

            nodelist = doc.GetElementsByTagName("ENTRY");

            foreach (XmlNode n in nodelist)
            {
                if (n.Attributes[0].InnerText.Equals(id))
                    termName = n.FirstChild.InnerText;
            }

            return termName;
        }

Thanks again guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.