List probleempje

Status
Niet open voor verdere reacties.

WtFudgE

Nieuwe gebruiker
Lid geworden
29 okt 2008
Berichten
1
Ik lees een xml file in, zou hem moeten sorteren op attribute "naam", maar daarna toch terug de value "id" terug vooraan krijgen. Ik heb dit momenteel zo opgelost:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XmlDocument document;
XmlNodeList nodelist;
List<string[]> list = new List<string[]>();

//make xmldocument & read the xml values
document = new XmlDocument();
document.Load("personen.xml");
nodelist = document.SelectNodes("/personen/persoon");
foreach (XmlNode node in nodelist)
{
string id = node.Attributes.GetNamedItem("id").Value;
string naam = node.ChildNodes.Item(0).InnerText;
string voornaam = node.ChildNodes.Item(1).InnerText;
list.Add(naam + ", " + voornaam + "@" + id);
}

//sort de list
list.Sort();

//write the list to a text file
StreamWriter SW;
SW = File.CreateText("personen.txt");
foreach (string[] line in list)
{
//use the '@' sign to sort the list but get the id to the front later on
int index = line.IndexOf('@');
string sub1 = line.Substring(index + 1);
string sub2 = line.Substring(0, index);
SW.WriteLine(sub1 + ", " + sub2);
}
SW.Close();
}
}
}

Ik vroeg me af of er een alternatieve methode voor bestaat om bv het @ teken te kunnen vermijden. Ik dacht eerst iets in deze style (zie hieronder), maar dan werkt mijn list.sort() niet meer.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XmlDocument document;
XmlNodeList nodelist;
List<string[]> list = new List<string[]>();

//make xmldocument & read the xml values
document = new XmlDocument();
document.Load("personen.xml");
nodelist = document.SelectNodes("/personen/persoon");
foreach (XmlNode node in nodelist)
{
string[] persoon = new string[3];
persoon[0] = node.ChildNodes.Item(0).InnerText;
persoon[1] = node.ChildNodes.Item(1).InnerText;
persoon[2] = node.Attributes.GetNamedItem("id").Value;
list.Add(persoon);
}

//sort de list
list.Sort();

//write the list to a text file
StreamWriter SW;
SW = File.CreateText("personen.txt");
foreach (string[] line in list)
{
SW.WriteLine(line[0] + line[1] + line[2]);
}
SW.Close();
}
}
}

bedankt voor reacties!
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan