Mega Code Archive

 
Categories / C# Book / 05 LINQ XML
 

0533 Get attribute out of element and change its value

using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; class Program { static void Main() { string xml = @"<Root><client enabled='false'></client></Root>"; XElement config = XElement.Parse(xml); XElement client = config.Element("client"); bool enabled = (bool)client.Attribute("enabled"); // Read attribute Console.WriteLine(enabled); // True client.Attribute("enabled").SetValue(!enabled); // Update attribute } } The output: False