CSharp - Creating XElement from String value

Description

Creating XElement from String value

Demo

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

class Program//w w w .  j a v  a  2 s  .c  o m
{
    static void Main(string[] args){
        XElement xBook = new XElement("Book");
        
        Console.WriteLine(xBook);
    }
}

Result

In the example, we instantiate an XElement object by passing the element's name as a string.

An XName object is created for us with a LocalName of Book and is assigned to the XElement object's Name property.

In this case, no namespace is provided, so the XName object has no namespace.

Related Topic