Muestra las diferencias entre dos versiones de la página.
| Próxima revisión | Revisión previa | ||
|
fw:othersnet:linqxml [2009/07/05 17:50] alfred creado |
fw:othersnet:linqxml [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| ====== Linq to XML ====== | ====== Linq to XML ====== | ||
| + | Es la capacidad que tiene .NET de acceder fácil y rápidamente a datos en formato xml. \\ | ||
| + | |||
| + | ===== Básico ===== | ||
| + | Pongamos el siguiente ejemplo de xml: | ||
| + | <code xml> | ||
| + | <?xml version="1.0" encoding="utf-8" ?> | ||
| + | <persons> | ||
| + | <person status="disabled"> | ||
| + | <name>Alfred</name> | ||
| + | <surname>GarGon</surname> | ||
| + | <projects> | ||
| + | <project>VS</project> | ||
| + | <project>Geisa</project> | ||
| + | </projects> | ||
| + | </person> | ||
| + | <person> | ||
| + | <name>Albert</name> | ||
| + | <surname>Dilbert</surname> | ||
| + | <projects> | ||
| + | <project>Temari</project> | ||
| + | </projects> | ||
| + | </person> | ||
| + | <person> | ||
| + | <name>Joan</name> | ||
| + | <surname>Nap</surname> | ||
| + | <projects> | ||
| + | <project>Temari</project> | ||
| + | <project>Geisa</project> | ||
| + | <project>VS</project> | ||
| + | </projects> | ||
| + | </person> | ||
| + | <person status="disabled"> | ||
| + | <name>Pepe</name> | ||
| + | <surname>Teca</surname> | ||
| + | <projects> | ||
| + | <project>VS</project> | ||
| + | </projects> | ||
| + | </person> | ||
| + | </persons> | ||
| + | </code> | ||
| + | |||
| + | ===== Consultar atributos y elementos ===== | ||
| + | Para abrir, cargar y consultar el archivo se utiliza la clase ''System.Xml.Linq.XDocument''. Luego querremos realizar consultas sobre los objetos person, por ejemplo los que esten con el atributo status enabled: | ||
| + | <code csharp> | ||
| + | XDocument xmlDoc = XDocument.Load("XMLFile1.xml"); | ||
| + | var persons = from person in xmlDoc.Descendants("person") | ||
| + | where ((person.Attribute("status") == null) || (person.Attribute("status").Value != "disabled")) | ||
| + | select new | ||
| + | { | ||
| + | name = person.Element("name").Value, | ||
| + | surname = person.Element("surname").Value | ||
| + | }; | ||
| + | |||
| + | foreach (var p in persons) | ||
| + | Console.WriteLine(p.name + " " + p.surname); | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ===== Consultar elementos anidados ===== | ||
| + | <code csharp> | ||
| + | XDocument xmlDoc = XDocument.Load("XMLFile1.xml"); | ||
| + | var persons = from person in xmlDoc.Descendants("person") | ||
| + | where ((person.Attribute("status") == null) || (person.Attribute("status").Value != "disabled")) | ||
| + | select new | ||
| + | { | ||
| + | name = person.Element("name").Value, | ||
| + | surname = person.Element("surname").Value, | ||
| + | projects = (from project in person.Elements("projects") | ||
| + | select project.Value).ToList() | ||
| + | }; | ||
| + | |||
| + | foreach (var per in persons) | ||
| + | { | ||
| + | Console.Write(per.name + ": "); | ||
| + | foreach(var proj in per.projects) | ||
| + | Console.Write(proj.ToString()); | ||
| + | Console.WriteLine(); | ||
| + | } | ||
| + | </code> | ||
| + | ===== Crear elementos ===== | ||
| + | |||
| + | ==== Crear fichero .xml ==== | ||
| + | Ejemplo: | ||
| + | <code csharp> | ||
| + | List<XElement> lAppsConf = getListXElements(user.AppsConf); | ||
| + | XDocument doc = new XDocument( | ||
| + | new XDeclaration("1.0", "utf-8", "true"), | ||
| + | new XElement("user", | ||
| + | new XElement("name", user.name), | ||
| + | new XElement("password", user.pass), | ||
| + | new XElement("default_app", user.defAppId), | ||
| + | new XElement("admin", user.admin.ToString()), | ||
| + | new XElement("apps_conf", lAppsConf))); | ||
| + | doc.Save(path); | ||
| + | </code> | ||
| + | |||
| + | Para agregar atributos podemos hacer lo siguiente: | ||
| + | <code csharp> | ||
| + | XElement element = new XElement("parada", | ||
| + | new XAttribute("parada", numParada.ToString()), | ||
| + | new XAttribute("descripcio", ""), | ||
| + | getBuses()); | ||
| + | </code> | ||
| + | ...O... | ||
| + | <code csharp> | ||
| + | element.SetAttributeValue("parada", "33"); | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ===== Notas ===== | ||
| + | * El método estático ''XDocument.Load'' recibe un string que corresponde a una URI; quiero decir, también podría aceptar una dirección externa a partir del protocolo http, por ejemplo. | ||
| + | * Para recoger el fichero xml podremos llamar al método ''Save'', pero si lo queremos en un string haremos: | ||
| + | <code csharp> | ||
| + | System.Text.StringBuilder output = new System.Text.StringBuilder(); | ||
| + | output.Append(doc.Declaration + Environment.NewLine); | ||
| + | output.Append(doc.ToString()); | ||
| + | string str = output.ToString(); | ||
| + | </code> | ||