CSharp - Updating an Attribute's Value

Description

Updating an Attribute's Value

Demo

using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

class Program/*from   w w w  . j  a  va 2  s . co  m*/
{
    static void Main(string[] args){
      //  we will use this to store a reference to one of the elements in the XML tree.
      XElement firstParticipant;

      XDocument xDocument = new XDocument(
        new XElement("Books", firstParticipant =
          new XElement("Book",
            new XAttribute("type", "Author"),
            new XAttribute("experience", "first-time"),
            new XElement("FirstName", "Joe"),
            new XElement("LastName", "Ruby"))));

      Console.WriteLine(System.Environment.NewLine +
        "Before changing attribute's value:");
      Console.WriteLine(xDocument);

      firstParticipant.Attribute("experience").Value = "beginner";

      Console.WriteLine(System.Environment.NewLine + "After changing attribute's value:");
      Console.WriteLine(xDocument);
    }
}

Result

Related Topic