CSharp - Creating Attributes with XAttribute

Introduction

An attribute implemented via XAttribute class is a name-value pair that is stored in a collection of XAttribute objects belonging to an XElement object.

We can create an attribute and add it to its element on the fly using functional construction.

Demo

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

class Program//from  ww w .  jav  a 2  s .  co  m
{
    static void Main(string[] args){
        XElement xBook = new XElement("Book",new XAttribute("type", "Author"));
        Console.WriteLine(xBook);
    }
}

Result

Related Topics