With things like this, C# is regaining my attention. Parse a simple XML to a string array containing only the child elements defined in an other string array:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace PruebaLinqFuncional
{
class Program
{
public static List ParseXML(string xml, string objectName, string[] dataToParse)
{
//Programacion funcional en C# :O
/*
return XDocument.Parse(xml).Descendants(objectName).Select((element) =>
{
var lst = new List();
foreach (string field in dataToParse)
lst.Add(element.Element(field).Value);
return lst.ToArray();
}).ToList();
*/
return XDocument.Parse(xml).Descendants(objectName).Select( (element) =>
{
return dataToParse.Select( (field) =>
{
return element.Element(field).Value;
}).ToArray();
}).ToList();
}
static void Main(string[] args)
{
var xml = @"
string-value
string-value
int-value
string-value
Mi_URL_Uno
Mi_Blob_Uno
date/time-value
etag
size-in-bytes
blob-content-type
Mi_URL_Dos
Mi_Blob_Dos
date/time-value
etag
size-in-bytes
blob-content-type
blob-prefix
";
foreach (var stra in ParseXML(xml, "Blob", new string[] { "Url", "Name" }))
Console.WriteLine(string.Join(",", stra));
Console.ReadLine();
}
}
}