Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how can i arrange data alphabetically?

how can i arrange data alphabetically?

Scheduled Pinned Locked Moved C#
questionxmlhelpannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    visiontec
    wrote on last edited by
    #1

    How can i arrange data in an xml file alphabetically?

    <?xml version="1.0" ?>
    <name>>Matt Daimon</name>
    <name>Kate Winsilet</name>
    <name>Martha Stewarts</name>
    <name>Will Smith</name>
    <name>Britiny Spears</name>
    <name>Jenifer Lopez</name>
    <name>Jenifer Aniston</name>
    </Names>

    Plz any sort of help will be gr8!

    H 1 Reply Last reply
    0
    • V visiontec

      How can i arrange data in an xml file alphabetically?

      <?xml version="1.0" ?>
      <name>>Matt Daimon</name>
      <name>Kate Winsilet</name>
      <name>Martha Stewarts</name>
      <name>Will Smith</name>
      <name>Britiny Spears</name>
      <name>Jenifer Lopez</name>
      <name>Jenifer Aniston</name>
      </Names>

      Plz any sort of help will be gr8!

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      That's a rather ambiqous question. Do you want to have it sorted in the file and rewritten? You can do that with an XSL Transform that looks like so:

      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output method="xml" version="1.0" standalone="yes"/>
      <xsl:template match="Names">
      <xsl:element name="Names">
      <xsl:apply-templates select="Name">
      <xsl:sort select="."/>
      </xsl:apply-templates>
      </xsl:element>
      </xsl:template>
      <xsl:template match="Name">
      <xsl:element name="Name">
      <xsl:value-of select="."/>
      </xsl:element>
      </xsl:template>
      </xsl:stylesheet>

      To transform the XML, you can do something simple like this:

      using System;
      using System.IO;
      using System.Xml;
      using System.Xml.XPath;
      using System.Xml.Xsl;
       
      class Test
      {
      static void Main(string[] args)
      {
      if (args.Length != 2) Environment.Exit(1);
      Transform(args[0], args[1]);
      }
       
      static void Transform(string xml, string xsl)
      {
      XmlDocument doc = new XmlDocument();
      doc.Load(xml);
       
      XslTransform tran = new XslTransform();
      tran.Load(xsl);
       
      tran.Transform(doc, null, Console.Out, null);
      }
      }

      If you just want to read the data in and sort it, you can do something like this:

      using System;
      using System.Collections;
      using System.IO;
      using System.Xml;
       
      class Test
      {
      static void Main(string[] args)
      {
      if (args.Length != 1) Environment.Exit(1);
      Sort(args[0]);
      }
       
      static void Sort(string file)
      {
      XmlDocument doc = new XmlDocument();
      doc.Load(file);
       
      XmlNodeList nodes = doc.DocumentElement.SelectNodes("/Names/Name");
      string[] names = new string[nodes.Count];
      int i=0;
      foreach (XmlNode node in nodes)
      names[i++] = node.InnerText;
       
      Array.Sort(names, Comparer.Default);
      foreach (string name in names)
      Console.WriteLine(name);
      }
      }

      There's practically an infinite amount of ways you can sort this data - it just depends on your needs. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups