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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:04:21+00:00 2026-05-26T07:04:21+00:00

I am trying to do a simple sort in a DataTable but have no

  • 0

I am trying to do a simple sort in a DataTable but have no success. The C# code is listed but what is needed for the corrections:

using System;
using System.Data;
using System.Xml;

namespace XMLParser
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            DataTable table = new DataTable();
            table.Columns.Add("code", typeof (string));

            table.Columns.Add("changePricePercentage", typeof (double));
            // Create a new XmlDocument  
            XmlDocument doc = new XmlDocument();

            // Load data  
            doc.Load(@"C:\Users\ratman\.jstock\1.0.6\Canada\watchlist\My Watchlist\realtimestock.xml");

            XmlNodeList nodes = doc.SelectNodes("//org.yccheok.jstock.engine.Stock");
            int x = 0;
            foreach (XmlElement element in nodes)
            {
                table.Rows.Add(
                    element.SelectSingleNode("code").InnerText,
                    element.SelectSingleNode("changePricePercentage").InnerText);
                Console.WriteLine(
                    "{0}: {1} {2}",
                    x,
                    element.SelectSingleNode("code").InnerText,
                    element.SelectSingleNode("changePricePercentage").InnerText);
                ++x;
            }
            Console.ReadKey();

            DataTable t = null;
            t = table.Copy();
            t.DefaultView.Sort = "[" + t.Columns[0].ColumnName + "] asc";

            //output to console for debugging
            for (int i = 0; i < t.Rows.Count; i++)
            {
                Console.WriteLine();
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    Console.Write(table.Rows[i].ItemArray[j].ToString() + "\t");
                }
            }

            Console.ReadKey();
        }
    }
}

I have looked everywhere for decent examples that actually work but found nothing.

  • 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-26T07:04:22+00:00Added an answer on May 26, 2026 at 7:04 am
    XmlNodeList nodes = doc.SelectNodes("//org.yccheok.jstock.engine.Stock");
    
    // Dynamically build your data table
    DataTable table = new DataTable();
    foreach (XmlElement field in nodes[0].SelectNodes(".//*[not(./*)]"))
        table.Columns.Add(new DataColumn(field.Name));
    
    // Populate with data
    foreach (XmlElement element in nodes)
    {
        DataRow row = table.NewRow();
        foreach (DataColumn column in table.Columns)
            row[column.ColumnName] = element.SelectSingleNode("//" + 
                column.ColumnName).InnerText;
        table.Rows.Add(row);
    }
    
    // Show sorted results
    table.DefaultView.Sort = "code asc";
    foreach (DataRowView row in table.DefaultView)
    {
        foreach (DataColumn column in table.Columns)
            Console.WriteLine("{0}: {1}", column.ColumnName, row[column.ColumnName]);
        Console.WriteLine();
    }
    
    Console.ReadKey();
    

    Seems you’re sorting correctly, but still getting your rows from an unsorted source.

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

Sidebar

Related Questions

I'm trying to write a simple implementation of a patience sort, using Scala. I've
I'm trying to accomplish a simple sort using parameters passed to my controller. I'm
I'm trying to get a simple sort function going using anonymous functions. One each
I'm trying to understand the follow code below: /** * Simple insertion sort. *
I am trying something very simple, but for some reason it does not work.
I am trying to write simple Visual Studio Add-In for code generation. In my
I'm killing myself and dehydrating trying to get this array to sort. I have
I'm trying to sort a simple array which only contains a few decimal numbers.
I'm trying to sort a vector of objects using a predicate function and I'm
I'm new to pl/sql ! I'm trying to sort a table of records, using

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.