visual valid remarks generate example documentacion comment c# xml linq linq-to-xml

c# - valid - Cómo convertir XML al diccionario



which example is a valid visual c# xml documentation comment (3)

Esta es una pregunta antigua, pero en caso de que alguien se encuentre con un xml ''Typed'' (p. Ej., De un archivo SharedPreference de una aplicación de Android), puede manejarlo de la siguiente manera: Aquí hay una muestra de un xml que tomé de una aplicación de Instagram . .

<?xml version=''1.0'' encoding=''utf-8'' standalone=''yes'' ?> <map> <boolean name="pinnable_stickers" value="false" /> <string name="phone_number">+254711339900</string> <int name="score" value="0" /> <string name="subscription_list">[]</string> <long name="last_address_book_updated_timestamp" value="1499326818875" /> //...other properties </map>

Note la inconsistencia en la propiedad de valor. Algunos campos (por ejemplo, de tipo string ) no lo tienen explícitamente definido.

var elements = XElement.Load(filePath) .Elements() .ToList(); var dict = new Dictionary<string, string>(); var _dict = elements.ToDictionary(key => key.Attribute("name").Value, val => val.Attribute("value") != null ? val.Attribute("value").Value : val.Value);

He xml como sigue:

<?xml version="1.0" encoding="UTF-8"?> <root> <data name="LogIn">Log In</data> <data name="Password">Password</data> </root>

Si lo logro sin Linq, cualquiera puede ayudarme a convertir el siguiente código a Linq:

using (XmlReader reader = XmlReader.Create(_xml)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "data") { reader.MoveToAttribute("name"); string key = reader.Value; reader.MoveToContent(); string value = reader.ReadElementContentAsString(); _dictionary.Add(key, value); } } reader.Close(); }


var xdoc = XDocument.Load(path_to_xml); _dictionary = xdoc.Descendants("data") .ToDictionary(d => (string)d.Attribute("name"), d => (string)d);


XDocument xdoc = XDocument.Load("test.XML"); var query = xdoc.Descendants("root") .Elements() .ToDictionary(r => r.Attribute("name").Value, r => r.Value);

Recuerde incluir:

using System.Linq; using System.Xml.Linq;