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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:39:56+00:00 2026-05-23T15:39:56+00:00

I have the following code in C# and .net 3.5 that is working fine

  • 0

I have the following code in C# and .net 3.5 that is working fine but need to know if this can be simplified may be using Linq or something else? Basically I am reading a xml file to get the column names. And then try to copy the columns and the sequence attribute in a dictionary object if “isactive attribute of the column is “true”. I use this dictionary object in other part of code. I loop through elements and then attributes and look for the columns that are active, if active I store the sequence and finally add the column and the sequence to the dictionary.

var doc = XDocument.Load("DataStructure.xml");
var cols = doc.XPathSelectElements("/datastructure/" + sPageName + "/columns");
Dictionary<string, int> columns = new Dictionary<string, int>();
bool bAddData = true;
int sSequence = 0;

foreach (var col in cols.Elements())
{
    foreach (XAttribute at in col.Attributes())
    {
        if (at.Name.ToString().ToLower().Equals("isactive") && at.Value.ToString() != "true")
        {
            bAddData = false;
            break;
        }

        bAddData = true;                    
        if (at.Name.ToString().ToLower().Equals("sequence"))
        {
            sSequence = Convert.ToInt32(at.Value.ToString());
            break;
        }
    }
    if (bAddData)
    {
        columns.Add(col.Name.ToString(), sSequence);
    }
}

I am sure this is pretty poor code but I would like to know how can I improve it. Here is the xml data file. I am ok if I need to restructure the xml to make this simple.

<?xml version="1.0" encoding="utf-8" ?>
<datastructure>
  <MyPage>
    <columns>
      <number isactive="true" sequence="1" />
      <curr_code isactive="true" sequence="2" />
      <curr_desc isactive="true" sequence="3" />
      <tradecurrvalue isactive="true" sequence="4" />
      <selectcurrvalue isactive="true" sequence="5" />
    </columns>    
  </MyPage>
</datastructure>
  • 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-23T15:39:56+00:00Added an answer on May 23, 2026 at 3:39 pm

    I think that this should do it:

    var doc = XDocument.Load("DataStructure.xml");
    var cols = doc.XPathSelectElements("/datastructure/" + sPageName + "/columns");
    
    Dictionary<string, int> columns =
      cols.Elements()
      .Where(c => c.Attribute("isactive").Value == "true")
      .ToDictionary(
        c => c.Name.ToString(),
        c => Int32.Parse(c.Attribute("sequence").Value)
      );
    

    Edit:

    Out of curiosity I wanted to find out what could be done about the original code without using LINQ once I figured out what it acutally did and how to best use the methods in the XElement class, and I arrived at this:

    var doc = XDocument.Load("DataStructure.xml");
    var cols = doc.XPathSelectElements("/datastructure/" + sPageName + "/columns");
    Dictionary<string, int> columns = new Dictionary<string, int>();
    
    foreach (var col in cols.Elements()) {
      if (col.Attribute("isactive").Value == "true") {
        columns.Add(col.Name.ToString(), Int32.Parse(col.Attribute("sequence").Value));
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code behind that was, until recently, working fine and dandy!
I have the following JQuery code that worked perfect in C#/Asp.net 2.0 to call
I am using ASP.NET 3.5 with iTextSharp and I have the following code: var
This is a beginner level question for asp.net MVC I have the following code
I have the following code that is working on my local machine (Win 7,
Anything I have tried didn't work. Currenly I have following code to change asp.net
I have the following code in my ASP.NET project public sealed class IoC {
I have the following code in a full .NET framework solution: public delegate int
i have the following code in an asp.net mvc view. <% = Html.DropDownList(Filter, new
I have the following code in my Site.Master page of an almost empty ASP.NET

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.