XDocument ใน System.Xml.Linq ใช้แทน XML Document สมบูรณ์ (มี Declaration, Processing Instructions, Comments) สืบทอดจาก XContainer จึงมี Child Nodes ได้ แต่ Root Element ต้องมีเพียง 1 ตัว
ใช้ XDocument.Parse()
using System;
using System.Xml.Linq;
namespace DemoApplication
{
class Program
{
static void Main()
{
string xmlString = @"
Account
Sales
Pre-Sales
Marketing
";
XDocument xml = XDocument.Parse(xmlString);
Console.WriteLine(xml);
Console.ReadLine();
}
}
}
ใช้ XDocument.Load() จากไฟล์
string xmlPath = @"D:\DemoXml.txt"; XDocument xml = XDocument.Load(xmlPath);
ทั้งสองวิธีจะได้ XDocument Object เหมือนกัน:

Tip: ถ้าไม่ต้องการ Declaration/Comments ให้ใช้ XElement แทน จะง่ายกว่า