Created
June 15, 2016 23:17
-
-
Save Fhernd/4d5c0657fe0430af2cfe21493dabdd8b to your computer and use it in GitHub Desktop.
Consultas LINQ sobre un documento XML. [OrtizOL]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Xml.Linq; | |
namespace Recetas.CSharp.R0615 | |
{ | |
public class ConsultaLinqSobreXml | |
{ | |
public static void Main() | |
{ | |
Console.WriteLine (); | |
// Carga del archivo XML: | |
XElement elementoRaiz = XElement.Load("store.xml"); | |
// Selecciona todos los productos de la categoría con ID 16: | |
IEnumerable<string> productosCat16 = from elemento in elementoRaiz.Elements() | |
where ( | |
elemento.Name == "Products" && | |
((string) elemento.Element("CategoryID")) | |
== "16") | |
select ((string)elemento.Element("ModelName")); | |
// Se muestra en la salida estándar todos los productos | |
// resultantes de la consulta LINQ anterior: | |
foreach (string valor in productosCat16) | |
{ | |
Console.WriteLine ("Producto en categoría 16: {0}", valor); | |
} | |
Console.WriteLine ("\nPresione Enter para continuar..."); | |
Console.ReadLine (); | |
// Consulta usando métodos de instancia: | |
productosCat16 = elementoRaiz.Elements().Where(elemento => elemento.Name == "Products" | |
&& (string) elemento.Element("CategoryID") == "16") | |
.Select(elemento => (string) elemento.Element("ModelName")); | |
// Visualización de la consulta: | |
foreach(string valor in productosCat16) | |
{ | |
Console.WriteLine ("Producto en categoría 16: {0}", valor); | |
} | |
Console.WriteLine (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment