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 7909705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:33:27+00:00 2026-06-03T12:33:27+00:00

Is there a better way to write the below program (I am sure it

  • 0

Is there a better way to write the below program (I am sure it can be but how)

for (int i = 0; i < relevantElements.Count(); i++)
            {
                switch (len)
                {
                    case 1: result.Add(new ContextElements
                    {
                        CultureName = GetElementValues(relevantElements[0], applicationType)
                        ,
                        AffId = null
                        ,
                        EmailAddress = null
                    }); 
                        break;
                    case 2:
                    result.Add(new ContextElements
                    {
                        CultureName = GetElementValues(relevantElements[0], applicationType)
                        ,
                        AffId = GetElementValues(relevantElements[1], applicationType)
                        ,
                        EmailAddress = null
                    }); 
                        break;
                    case 3:
                        result.Add(new ContextElements
                        {
                            CultureName = GetElementValues(relevantElements[0], applicationType)
                            ,
                            AffId = GetElementValues(relevantElements[1], applicationType)
                            ,
                            EmailAddress = GetElementValues(relevantElements[2], applicationType)
                        });
                        break;
                }
            }

Thanks

Edit

I have a source xml file

<?xml version="1.0" encoding="utf-8"?>

<RootElement>

  <HomeSite>

   <Context enabled="1" active="1">

      <Culture>en-us</Culture>

      <Affiliate>0</Affiliate>

      <EmailAddress>sreetest@test.com</EmailAddress>

      <Password>sreesree1</Password>

    </Context>

  </HomeSite>

  <ResourceManager>

    <Context enabled="1" active="1">

      <Culture>en-us</Culture>

      <Affiliate>0</Affiliate>

    </Context>

  </ResourceManager>  

  <Purchase>

     <Context enabled="1" active="1">

      <PurchaseUrl>http://purchase.mcafee.com?culture=en-us&amp;affid=0</PurchaseUrl>

    </Context>

    <Context enabled="0" active="0">

      <PurchaseUrl>http://purchase.mcafee.com?culture=en-gb&amp;affid=0</PurchaseUrl>

    </Context>

  </Purchase>  

</RootElement>

As can be figure out for “HomeSite” Applcation Type, there are 3 elements under “Context” element viz Culture, Affiliate,EmailAddress . But this changes from other Application Types “ResourceManager” and “Purchase”. I also have a custom entity as under

public class ContextElements

{
    public string CultureName { get; set; }

    public string AffId { get; set; }

    public string EmailAddress { get; set; }

}

The challenge is that i have to fill up the property values at runtime. It cannot be more than 3.

My complete program (but needs an improve code)

class Program
    {       
        static void Main(string[] args)
        {
            XDocument xmlSkuDescDoc = null;

            xmlSkuDescDoc = XDocument.Load(@"D:\Config.xml");

            string applicationType = ApplicationType.Purchase.ToString();

            var result = new List<ContextElements>();

            var elementCollection = new List<string>();


            (from data in xmlSkuDescDoc.Descendants(applicationType)
             select data)
                     .Descendants("Context")
                     .Elements()
                     .ToList()
                     .ForEach(i => elementCollection.Add(i.Name.ToString()));

           //Exclude unwanted elements
            var relevantElements = elementCollection.Except(new List<string> { "Password" }).ToList();

            int len = relevantElements.Count();

            for (int i = 0; i < relevantElements.Count(); i++)
            {
                switch (len)
                {
                    case 1: result.Add(new ContextElements
                    {
                        CultureName = GetElementValues(relevantElements[0], applicationType)
                        ,
                        AffId = null
                        ,
                        EmailAddress = null

                    }); 
                        break;
                    case 2:
                    result.Add(new ContextElements
                    {
                        CultureName = GetElementValues(relevantElements[0], applicationType)
                        ,
                        AffId = GetElementValues(relevantElements[1], applicationType)
                        ,
                        EmailAddress = null
                    }); 
                        break;
                    case 3:
                        result.Add(new ContextElements
                        {
                            CultureName = GetElementValues(relevantElements[0], applicationType)
                            ,
                            AffId = GetElementValues(relevantElements[1], applicationType)
                            ,
                            EmailAddress = GetElementValues(relevantElements[2], applicationType)
                        });
                        break;
                }
            }

           // //just for printing
           result.ForEach(i => Console.WriteLine(string.Format("CultureName = {0} , AffId = {1} , EmailAddress = {2} ", i.CultureName, i.AffId, i.EmailAddress)));

            Console.ReadKey();

        }

        private static string GetElementValues(string elementName,string applicationType)
        {
             XDocument xmlSkuDescDoc = null;

            xmlSkuDescDoc = XDocument.Load(@"D:\Config.xml");

            return (from data in

                             (from data in xmlSkuDescDoc.Descendants(applicationType)

                              select data).Descendants("Context")

                    select new { Value = (string)data.Element(elementName)}).FirstOrDefault().ToString();
        }

        public enum ApplicationType
        {
            HomeSite = 1
            ,
            ResourceManager
                ,
            ApplicationProvisioning
                ,
            Purchase
                ,
            GAC
           , WebSVCService
        }
    }
  • 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-03T12:33:30+00:00Added an answer on June 3, 2026 at 12:33 pm

    A straight mechanical rewrite of the code with identical behavior would be

    for (int i = 0; i < relevantElements.Count(); i++)
    {
        result.Add(new ContextElements
        {
            CultureName = GetElementValues(relevantElements[0], applicationType),
            AffId = len < 2 ? null : GetElementValues(relevantElements[1], applicationType),
            EmailAddress = len < 3 ? null : GetElementValues(relevantElements[2], applicationType),
        }); 
    }
    

    That said, I have no idea what this code is trying to do, or if it does so correctly.

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

Sidebar

Related Questions

Is there a better way to write the below, other then using a switch
I was wondering if there is a better way to write my code below.
Is there a better/shorter way to write the whoAmI method in the following code?
is there any way to write this foreach in linq or another better way,
The first one below works, but, just wanted to see if there's a better
Is there better way to delete a parameter from a query string in a
Is there better way to enumerate all photos on the device than this one?
Is there a better way to generate HTML email in C# (for sending via
Is there a better way than using globals to get interesting values from a
Is there a better way to control the space between certain block elements.. i

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.