0

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>
3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by mduncan8
0

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

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

0

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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.