Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7803635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:38:54+00:00 2026-06-02T01:38:54+00:00

In my MVC application, I have a service call (http://dev-service.test.com/api/brands?active=true) that returns the following

  • 0

In my MVC application, I have a service call (http://dev-service.test.com/api/brands?active=true) that returns the following XML

<Brands>
  <Brand>
  <BrandId>1</BrandId>
  <BrandNo>20</BrandNo>
  <BrandName>ABC</Domain>
  </Brand>

  <Brand>
  <BrandId>2</BrandId>
  <BrandNo>30</BrandNo>
  <BrandName>XYZ</Domain>
  </Brand>
<Brands>

In one of my user controls, I would like to populate a dropdownlist with the BrandName values. I already have a ViewModel that contains a bunch of properties. How can I populate the dropdown with the values from this XML?

P.S: I am new to MVC and still learning the basics of viewmodels etc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:38:55+00:00Added an answer on June 2, 2026 at 1:38 am

    There really are 2 parts in your question. The XML parsing part (which has strictly nothing to do with ASP.NET MVC) and the ASP.NET MVC part. Since your question is tagged with asp.net-mvc let’s first answer this part. So you mention a view model. Something like this:

    public class BrandsViewModel
    {
        public string Brand { get; set; }
        public IEnumerable<SelectListItem> Brands { get; set; }
    } 
    

    then a controller action:

    public ActionResult Index()
    {
        BrandsViewModel model = ...
        return View(model);
    }
    

    and finally the view part:

    @model BrandsViewModel
    @using (Html.BeginForm())
    {
        @Html.DropDownListFor(x => x.Brand, Model.Brands)      
        <button type="submit">OK</button>
    }
    

    Alright, this is where the ASP.NET MVC part ends in your question. Now comes the XML parsing part. There are several ways to parse XML in C#. For example you could use the XDocument class.

    Of course before being able to parse XML you need to have XML. What you have shown in your question is not XML. It is a string. You need to first fix it and have a valid XML. Like this:

    <Brands>
      <Brand>
        <BrandId>1</BrandId>
        <BrandNo>20</BrandNo>
        <BrandName>ABC</BrandName>
      </Brand>
    
      <Brand>
        <BrandId>2</BrandId>
        <BrandNo>30</BrandNo>
        <BrandName>XYZ</BrandName>
      </Brand>
    </Brands>
    

    Now that you have valid XML let’s move on and use a XML parser.

    var brands =
        from brand in XDocument.Load("brands.xml").Descendants("Brand")
        select new SelectListItem
        {
            Value = brand.Element("BrandId").Value,
            Text = brand.Element("BrandName").Value
        };
    

    and now let’s make the 2 work together:

    public ActionResult Index()
    {
        var brandsFile = Server.MapPath("~/app_data/brands.xml");
        var brands =
            from brand in XDocument.Load(brandsFile).Descendants("Brand")
            select new SelectListItem
            {
                Value = brand.Element("BrandId").Value,
                Text = brand.Element("BrandName").Value
            };
    
    
        var model = new BrandsViewModel
        {
            Brands = brands
        };
        return View(model);
    }
    

    This is where we can see that we have strongly coupled out controller action logic with XML parsing logic which is bad. You could introduce an abstraction (interface) which will be injected into the controller’s constructor and then used by the action. Then you could provide a specific implementation of this abstraction that will perform the actual XML parsing and configure your dependency injection framework to pass it to the controller.

    So let’s do that. Let’s define a domain model that will represent our brands:

    public class Brand
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
    

    Cool. Now what do we want to do with those brands? Retrieve a list of them. Let’s define our contract:

    public interface IBrandsRepository
    {
        Brand[] Get();
    }
    

    OK, we have specified what operations we need with our brands. Now we could have our controller look like this:

    public class BrandsController: Controller
    {
        private readonly IBrandsRepository _repository;
        public BrandsController(IBrandsRepository repository)
        {
            _repository = repository;
        }
    
        public ActionResult Index()
        {
            var brands = _repository.Get().Select(b => new SelectListItem
            {
                Value = b.Id,
                Text = b.Name
            });
            var model = new BrandsViewModel
            {
                Brands = brands
            };
            return View(model);
        }
    }
    

    There is still room for improvement in this controller action. Notice that we are querying the repository and obtaining a list of domain models (Brand) and converting this domain model into a view model. This is cumbersome and pollutes our controller logic. It would be better to externalize this mapping into a separate layer. Personally I use AutoMapper for that. It’s a lightwight framework which allows you to define the mappings between different classes in a fluent way and then simply pass it instances of the source type and it will spit you instances of the target type:

    public class BrandsController: Controller
    {
        private readonly IBrandsRepository _repository;
        public BrandsController(IBrandsRepository repository)
        {
            _repository = repository;
        }
    
        public ActionResult Index()
        {
            var brands = _repository.Get();
            var model = new BrandsViewModel
            {
                Brands = Mapper.Map<IEnumerable<Brand>, IEnumerable<SelectListItem>>(brands)
            };
            return View(model);
        }
    }
    

    So we are making progress here. Now we could have an implementation of our contract:

    public class BrandsRepositoryXml: IBrandsRepository
    {
        private readonly string _brandsFile;
        public BrandsRepositoryXml(string brandsFile)
        {
            _brandsFile = brandsFile;
        }
    
        public Brand[] Get() 
        {
            return
                (from brand in XDocument.Load(_brandsFile).Descendants("Brand")
                 select new Brand
                 {
                     Id = brand.Element("BrandId").Value,
                     Name = brand.Element("BrandName").Value
                 })
                 .ToArray();
        }
    }
    

    And the last step of the puzzle is to configure some DI framework to inject the proper implementation of our contract into the controller. There are like a gazzilion of DI frameworks for .NET. Just pick one. It doesn’t really matter. Try the Ninject.MVC3 NuGet. It’s kinda cool and easy to setup. Or if you don’t want to use third party DI frameworks simply write a custom dependency resolver.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an asp.net MVC application that calls another service to generate a pdf.
I have an ASP.NET MVC 3 application that uses a WCF service within the
In my ASP .NET MVC application i have a link that refreshes the preview
I have a spring mvc application that I have broken up into separate maven
I have an MVC application view that is generating quite a large HTML table
I have an MVC application that needs to login and verify a user against
I have an mvc application that has been coded to use Windows authentication and
I have an ASP.NET MVC application that currently uses the WebClient class to make
In my MVC application I have the following mapping: routes.MapRoute(testRoute, services/service.js, new {controller =
I have an existing ASP.NET MVC 2 application that I've been asked to extend.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.