xdocument query node name from example c# find xelement

c# - query - Si: XElement Exists, Else Create new one



xdocument get element by name (4)

Mi archivo XML:

<?xml version="1.0" encoding="utf-8"?> <SimpleKD> <player name="Tardis"> <kills>0</kills> <deaths>0</deaths> </player> </SimpleKD>

Primero, necesito verificar si el nombre de usuario "Tardis" existe en el .element("player").attribute("name") xml .element("player").attribute("name") .

Si no es así, necesito crearlo con muertes y muertes en cero. Si lo hace, necesito leer muertes y muertes y establecerlas en variables.

He estado usando XElement para intentar hacer esto ... ¡Gracias!

Código utilizado para escribir el XML:

public static string username = "Tardis"; public static int kills = 0; public static int deaths = 0; ........ XElement Players = new XElement( "SimpleKD", new XElement("player", new XAttribute("name", username), new XElement("kills", kills), new XElement("deaths", deaths)));


Suponiendo que tienes una clase de Player :

private static Player GetPlayer(string name, XElement simpleKD) { var playerElem = simpleKD.Elements("player") .SingleOrDefault(p => p.Attribute("name").Value == name); if (playerElem == null) { simpleKD.Add(new XElement("player", new XAttribute("name", name), new XElement("kills", 0), new XElement("deaths", 0))); return new Player(name); } return new Player(name, (int)playerElem.Element("kills"), (int)playerElem.Element("deaths")); }


Si el reproductor está en cualquier parte de xml, utiliza simpleKD.Descendants. Si existe como hijos inmediatos, utiliza elementos simples KD.Elements

private Player GetPlayer(string name, XElement simpleKD) { var playerElem = simpleKD.Descendants("player").SingleOrDefault(x => x.Attribute("name").Value.Equals(name)); if (playerElem == null) { simpleKD.Add(new XElement("player", new XAttribute("name", name), new XElement("kills", 0), new XElement("deaths", 0))); return new Player(name); } return new Player(name, (int)playerElem.Element("kills"), (int)playerElem.Element("deaths")); } class Player { public string Kills { get; set; } public string Deaths { get; set; } public Player() { } public Player(string name) { } public Player(string name, int kills, int deaths) { } }


int k = 0, d = 0; //kills and deaths var doc = XDocument.Parse(xmlString); var elem = doc.XPathSelectElement("/SimpleKD/player[@name=''Tardis'']"); if (elem != null) { var killsNode = (XElement)elem.FirstNode; k = (int)killsNode; var deathsNode = (XElement)killsNode.NextNode; d = (int)deathsNode; } else { elem = doc.XPathSelectElement("/SimpleKD"); elem.Add(new XElement("player", new XAttribute("name", "Tardis"), new XElement("kills", k), new XElement("deaths", d))); } xmlString = doc.ToString(); Console.WriteLine("Kills: {0}", k); Console.WriteLine("Deaths: {0}", d);


XmlDocument xmlfile= new XmlDocument(); xmlfile.Load("FileName.xml"); XmlNode node = xmlfile.SelectSingleNode("//SimpleKD"); XmlElement element = (XmlElement)node.SelectSingleNode(string.Format("//player[@name=''{0}'']", "Tardis")); if(element == null) { element = node.CreateElement("player"); element .SetAttribute("name", "Tardis"); node.AppendChild(element); xmlfile.Save("FileName.xml"); XmlElement newElement = new Element(); newElement = node.CreateElement("Kills"); newElement.Value = 0; element.AppendChild(newElement); newElement = node.CreateElement("Deaths"); newElement.Value = 0; element.AppendChild(newElement); xmlfile.Save("FileName.xml"); } else { kills = element.FirstChild.Value; deaths = element.LastChild.Value; }