0

I am creating a User Registration form in ASP.NET MVC3 using razor view engine. I am facing problem to create a dropdownlist for country. Country list come from xml file.

My project hierarchy is as follows

BusinessLayer -> User_Account -> Account_Registration.cs

This is a class library in which I want to create a Model for user registration. The code for user model is as follows

public class Account_Registration
{
    public string User_Name { get; set; }
    public string User_EmailID { get; set; }
    public string User_Password { get; set; }
    public string User_RePassword { get; set; }
    public DateTime User_BirthDate { get; set; }
    public enum_Gender User_Gender { get; set; }
    public string User_Address { get; set; }
    public string User_City { get; set; }
    public string User_State { get; set; }
    public IEnumerable<SelectListItem> User_Country { get; set; }
    public string User_WebSite { get; set; }
    public string User_Description { get; set; }
}

Now I want to know that where I should put country XML file and how can I create dropdownlist for XML file. My Xml file is as follows

<countries>
      <country code="AF" iso="4">Afghanistan</country>
      <country code="AL" iso="8">Albania</country>
      <country code="DZ" iso="12">Algeria</country>
</countries>

As I have to deploy this project on IIS so I want to know where should I put xml file so that I can access it in Account_Registration model which is in class library project and how to create dropdownlist for population countries. Thanks

8
  • Why don't you just import the xml file into your database? Commented Mar 31, 2012 at 16:56
  • Import xml in table or content only Commented Mar 31, 2012 at 16:57
  • Do you have a repository? Write a method in your repository that deserializes the XML file and returns a List<SelectListItem>. Better yet, put the countries in the database, as @MystereMan suggests. Commented Mar 31, 2012 at 16:58
  • how to create a repository. Please let me know how to create it Commented Mar 31, 2012 at 17:04
  • What are you using for your Business Layer? EF? Linq to SQL? Commented Mar 31, 2012 at 17:05

2 Answers 2

1

You probably shouldn't read xml file each time you render registration page. That would be one little bottleneck you'd have since hard drive operations are costly. I'd recommend reading it into memory (like at the application startup once and somewhere into the global variable, e.g. Countries).

For rendering your list, I'd recommend looking at the following article. Basically, it goes like this:

 Html.DropDownList(“countries”, new SelectList(model.Countries), “CountryId”, “CountryName”))
Sign up to request clarification or add additional context in comments.

4 Comments

It's a bottleneck because XML deserialization can be expensive. If it were in the database, you'd still need to read it from the disk. :)
Not really. Databases can cache frequently-accessed data in memory for one and they use special techniques for fetching data from disk (more like finding data on disk) faster. Deserialization may be expensive and may be not :)
Well, yes, but you can cache the result of an XML deserialization also. It really depends on what the OP is after. If the OP wants to change the configuration via a text file, rather than building the infrastructure to maintain the database records in a form, an XML file might be appropriate. Personally, I'd just add the records to a database table manually, and be done with it.
And I've just suggested caching xml results into the memory. :) Regarding database - agree.
0

you can create own extension for DropDown.

public static class GridExtensions
{
    public static MvcHtmlString XmlDropDown(this HtmlHelper helper, string name, string value)
    {
        var document = XDocument.Parce(value);
        var model = new List<SelectListItem>();
        foreach(XElement element in document.Elements("countries/country"))
        {
            model.Add(new SelectListItem(){Text=element.Value, Value=element.Attribute("iso").Value})
        }

        return Html.DropDownList(name, model))
    }
}

So, on view you can use

Html.XmlDropDown(“countries”, model.Countries)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.