as beginner in c# : I googled around but got lost in a "simple question" :
Read a XML :
<persons> this is sample
<person id="0">
<Lname>Johnson</Lname>
<Fname>Molly</Fname>
</person>
<person id="1">
<Lname>buffalo</Lname>
<Fname>Mike</Fname>
</person>
<person id="2">
<Lname>COOLS</Lname>
<Fname>WALTER</Fname>
</person>
<person id="3">
<Lname>FROMUS</Lname>
<Fname>LUDOVICUS</Fname>
</persons>
I need to sort on Lname (last name) and Fname (first name)
and list up in a text file (txt) to
have
E.G. yo see 2 people have Lname of York so then sort on Fname.
Case John
Buffalo Mike
York Theo
York Viviane
Lname and Fname should be on same line but take a new line for next Lname , Fname.
thats not in this question because of syntax problems here in this tool (sorry)
Additional info : I tried to use foreach with var instruction to list up but got messed. Target is to use e.g LINQ and OO (object oriented ) as much as possible .
thanks a lot
My response to the comments :
First I want to apologize for the badformed XML -the last tag had a wrong / symbol. The language was indeed dutch (Netherlands) - I changed tag names into english.
What I already have is reading of XML file and sorting on attribute "id" and write into a flat file. Then I tried to make it more OO by exercising the "foreach var constructions using orderedby .
Code I have now :
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Xml;
using System.Net;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create("c:/download/test.xml");
List<string> Collectperson = new List<string>();
Collectperson.Clear();
string personid = "";
string nm = "";
string vn = "";
string oneline = "";
while (reader.Read())
{
if (reader.NodeType != XmlNodeType.EndElement)
{
// Get element name and switch on it.
switch (reader.Name)
{
case "persoon":
// Detect this element.
// initialize collectperson
Collectperson.Clear();
Console.WriteLine(" " + reader.GetAttribute(0));
Collectperson.Add(reader.GetAttribute(0));
// add to file ans reinitialize string
oneline = "";
oneline = string.Join(" ", Collectperson.ToArray());
break;
//
case "naam":
if (reader.Read())
{
Console.WriteLine(reader.Value);
nm = reader.Value;
Collectperson.Add(nm);
oneline = string.Join(" ", Collectperson.ToArray());
}
break;
case "voornaam":
if (reader.Read())
{
Console.WriteLine(reader.Value);
vn = reader.Value;
Collectperson.Add(vn);
oneline = string.Join(" ", Collectperson.ToArray());
// ==================================================================================
string path = @"c:/download/personen.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(oneline);
}
}
// This text is always added, making the file longer over time
// if it is not deleted.
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(oneline);
}
//==================================================================================
}
break;
} // switch
}
} // while
}
}
}