Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
fw:othersnet:linqxml [2009/07/05 18:10] alfred |
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. \\ | Es la capacidad que tiene .NET de acceder fácil y rápidamente a datos en formato xml. \\ | ||
| + | |||
| ===== Básico ===== | ===== Básico ===== | ||
| Línea 35: | Línea 36: | ||
| <surname>Teca</surname> | <surname>Teca</surname> | ||
| <projects> | <projects> | ||
| - | <project>Geisa</project> | + | <project>VS</project> |
| </projects> | </projects> | ||
| </person> | </person> | ||
| Línea 56: | Línea 57: | ||
| Console.WriteLine(p.name + " " + p.surname); | Console.WriteLine(p.name + " " + p.surname); | ||
| </code> | </code> | ||
| + | |||
| + | |||
| ===== Consultar elementos anidados ===== | ===== 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 ===== | ===== 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. | + | * 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> | ||