Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have an XML-file with around 20Lakhs records.
I wish generate an Auto-Complete method for a textBox.
For Performing faster Index-based-Search in XML-file which will be the better option.
1.) XPath (or)
2.) LINQ
Or Any other technique is there.. for more faster search? Plz suggest and Explain...
Posted

If you're searching for a small portion of data in you XML document, I'd say that using XmlReader[^] provides the fastest access to the data.

However since you're using it to create an auto-complete text box I guess that the search is going to be repeated often. Because of this it could be reasonable to load and parse the XML document and have an in-memory copy of the data in such format which is both fast to search and compact in size. Utilizing a HashSet[^] could be one option.
 
Share this answer
 
Comments
Albin Abel 2-Apr-11 3:21am    
my 5. Added point. If it is a asp.net application then need to keep the collection a static one. Otherwise the collection has to be reconstructed for every page requests. OP hasn't mentioned it is a windows or asp.net application
Wendelius 2-Apr-11 3:55am    
Thank you :) Yes, you're right, if ASP then the collection should be static.
SHAJANCHERIAN 2-Apr-11 4:27am    
I am using Ajax-AutoComplete Extender and a WebService: Hope It will Manage In-memory collection.
Request for your suggestion/ advice..
------------------------------------------------------------------------------------------------
<cc1:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" TargetControlID="TextBox2"
ServiceMethod="GetItems" MinimumPrefixLength="1" CompletionSetCount="10" EnableCaching="true"
ServicePath="WebService.asmx">

-------------------------------------------------------------------------------
[WebMethod]
public string[] GetItems(string prefixText, int count)
{
List<string> suggestions = new List<string>();
using (XmlTextReader reader = new XmlTextReader(HttpContext.Current.Server.MapPath("~/XmlFiles/Items.xml")))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "item")
{
string itemName = reader.ReadInnerXml();
if (itemName.StartsWith(prefixText, StringComparison.InvariantCultureIgnoreCase))
{
suggestions.Add(itemName);

if (suggestions.Count == count) break;
}
}
}
}
return suggestions.ToArray();
}
------------------------------------------------------------------------------------------
Wendelius 2-Apr-11 8:26am    
One possible bottleneck in this scenario is the web service. Consider if you could searxh for the data without having to call the web service each time the data changes (if I understood your approach correctly).
Albin Abel 3-Apr-11 15:16pm    
Hi there is a over head of a web service call. Web service is fine for a layered application where many clients need to access a common resource. But again in your implementation the results are not cached in memory. Every time it reads the xml file and do all blah blah. Keep the suggestions as a static list that would be the best suggestion for this scenario. If multiple web application going to use this list then you can serve this list through a web service, else move it to your own application will give you performance.
Hi
The autocomplete extender works with a webservice or a page method decorated with webservice method and script service method attributes. To read an XML with that much record from file and search for each character typed is a huge work. An in memory collection may help a little. Whether xpath or linq, it has to be executed after the data loaded into memory. So if you have that already in memory this overhead can be avoided. But remember this is again a trade off between speed and memory consumption. The in memory collection permanently occupy some memory space. Again it is a string collection definitely occupy more space. The key parameter you can set in the autocomplete markup is MinimumPrefixLength. It should be minimum 3 to avoid a costly search.

Another way store the xml in sql server and when needed retrieve it and can use the xpath. Which will be more faster than file based. Refer this link. http://msdn.microsoft.com/en-us/library/ms187508%28v=sql.90%29.aspx[^]

Well google give more information if you use key words "ado.net xml best practice".

You asked for a code. Well here is the code for in memory collection

C#
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class TestService : System.Web.Services.WebService {
    //it is a static array which will be available at the application scope. Not session based.
    private static string[] suggestions;

    [WebMethod]
    public string[] getSuggestions(string prefixText, int count)
    {
        //static array will be populated only one time.
        if (suggestions == null)
        {
            setSuggestions();
        }
        return suggestions.Where(x => x.StartsWith(prefixText)).ToArray<string>();
    }
    //if some instance the list corrupted can be reset.
    [WebMethod]
    public void ResetSuggestions()
    {
        setSuggestions();
    }
    private void setSuggestions()
    {
        IList<string> templist = new List<string>();
        //have your xml reading code here to populate the array.
        templist.Add("happy");
        templist.Add("world");
        suggestions = templist.ToArray();
    }
}


But the important thing don't just rely on the code blindly. Follow my guidelines above, read more and decide which one fit for your case. Try yourself and then come back if you have any problem with code.
 
Share this answer
 
Comments
SHAJANCHERIAN 15-Apr-11 9:40am    
Thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900