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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:00:37+00:00 2026-05-23T17:00:37+00:00

I made a little code gen and was wondering if I should go to

  • 0

I made a little code gen and was wondering if I should go to the trouble of handling the extra comma at the end. IE seems to ignore it, but I need to keep it cross browser, and I’d like to generate valid code anyway.

function init() {
var myOptions = { : 'Select home value', // <== THERE'S THE NON EXISTANT KEY
100000 : '$90,001 - $100,000',
1000000 : '$950,001 - $1,000,000',
1000001 : 'Over $1,000,000', // <== HERE'S THE COMMA I'M CURIOUS ABOUT
};

here’s the code to generate

protected string DoTransform()
{
    var sb = new StringBuilder("var myOptions = {");
    foreach (var option in 
        XDocument.Load(MapPath("~/App_Data/Data.xml"))
            .XPathSelectElements("./data/options[@question='ApproximatePropertyValue']/option"))
    {
        sb.AppendFormat("{0} : '{1}',\n", option.Attribute("value").Value, option.Value);
    }
    sb.AppendLine("};");
    return sb.ToString();
}

ANSWER:
Here’s the updated code that takes care of the empty key (by skipping the first element) and trailing comma (by rearranging logic so TrimEnd can nab it).

protected string DoTransform()
{
    var sb = new StringBuilder();
    foreach (var option in 
        XDocument.Load(MapPath("~/App_Data/Data.xml"))
            .XPathSelectElements("./data/options[@question='ApproximatePropertyValue']/option")
            .Skip(1))
    {
        sb.AppendFormat("{0}:'{1}',", option.Attribute("value").Value, option.Value);
    }
    return"var myOptions = {\n" + sb.ToString().TrimEnd(',') + "};";
}
  • 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-05-23T17:00:38+00:00Added an answer on May 23, 2026 at 5:00 pm

    My understanding is that most browsers will allow the trailing comma, but it is not something which is acceptable in the JSON spec, so it is a bad idea. On the other hand, the fact that you are missing a key in that first key-value pair won’t be forgiven… by ANYONE 😉

    Edit

    Just saw your code above. Forgive me, my .NET is rusty (as in, I’ve barely looked at it, ever), but I believe this will work:

    foreach (var option in 
        XDocument.Load(MapPath("~/App_Data/Data.xml"))
           .XPathSelectElements(
                 "./data/options[@question='ApproximatePropertyValue']/option"
           )
        )
    {
        // use the length as a flag, if you've added to it, it will be longer than 16
        if(sb.Length > 20)sb.Append(",");
        sb.AppendFormat("\n{0} : '{1}'", 
            option.Attribute("value").Value, option.Value);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made this little function from code snippets around the net. It does what
So I have made a little piece of code that will insert and manipulate
I need to know where my users are from, so I made a little
I made a little code for creating a matrix of coordinates (like a chessboard),
I made this little piece of code where I alloc and then free memory
So I made a little game in java with slick2D, but I want my
Im trying to add a little code to an already made thumbnail plugin. Basically
I made a little Shooter game with two ships firing at each other. I
I've made a little forum and I want parse the date on newest posts
Made this nice little loop for hiding and showing div's, works as a charm

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.