Refactor Code

  • Gravatar
    Filling a DropDownList Using XML

    by azamsharp on 7/8/2008 6:48:34 PM
  • Some time back I had the requirement to fill the DropDownList control using XML file. Here is the code:
  • // The XML file
    <?xml version="1.0" encoding="utf-8" ?>

    <names>

    <name>

    <client>hank</client>

    </name>

    <name>

    <client>corry</client>

    </name>

    <name>

    <client>david</client>

    </name>

    <name>

    <client>james</client>

    </name>

    </names>

    And here is the C# code:

    XmlDocument doc = new XmlDocument();

    doc.Load(Server.MapPath("Menu.xml"));

    XmlNodeList nodeList = doc.SelectNodes("names/name");

    foreach(XmlNode node in nodeList)

    DropDownList1.Items.Add(new ListItem(node.SelectSingleNode("client").InnerText));
  • Refactor it!
  • Gravatar
    i have used databinding for refactoring the code instead of looping the collection
    by virangpatel2007 on 7/15/2008 11:28:45 PM
  • DataSet ds = new DataSet();
    DataTable sourceData = new DataTable(); ds.ReadXml(Server.MapPath("XMLFile2.xml")); drpfromxml.DataSource=ds; drpfromxml.DataMember=ds.Tables[0].ToString();
    drpfromxml.DataTextField = "client";
    drpfromxml.DataValueField = "client";
    drpfromxml.DataBind();
  • Gravatar
    I guess both of our code suffers from the same problem which is that the XML file is first loaded into memory and then populated into the dropdownlist control. The ideal solution is to just go over the XML file using a reader and populate the dropdownlist. This way no memory will be required to hold all the contents of the XML file.
    by azamsharp on 7/16/2008 7:36:31 PM
  • Gravatar
    Out of curiosity, if a file were so large that it would be more practical to use a reader rather than loading the whole thing in memory, would that then mean the file was a bit too large for use in a dropdown list or perhaps that a database would be a more convenient method for holding the data?
    by dudeserius on 7/21/2008 12:38:42 PM
  • Gravatar
    By file being too large I meant too large to load in the memory quickly. It really depends what your server configuration is but if you file is like 1-2 MB then it is just too large to fit in the memory. Better use a database and load the records from there instead of the file.
    by azamsharp on 7/21/2008 12:43:35 PM
  • Gravatar
    That makes sense. I'm normally just an app developer, but have been venturing into web development as well as of late, and topics such as this interest me. What do you esteem to be the borderline file size of an xml document where you would need to consider streaming the file rather than loading the whole thing into memory?
    by dudeserius on 7/21/2008 12:53:29 PM
  • Gravatar
    Hi, Well, that will be hard to estimate. Let's put it this way that if the data in the XML file is constant for a long periods of time like "Menu links" for the website then it is suitable to load the xml into a container and cache it. The dependency of the cache will be on the xml file. Off course the xml file will be small under 5-10 KB. If the data is large and constantly changing then it is a better idea to store it in the persistant storage like database.
    by azamsharp on 7/21/2008 2:14:00 PM
Please log in to refactor the code! Login