How to: Load XML from a File

This topic shows how to load XML from a URI by using the XElement.Load method.

The following example shows how to load an XML document from a file. The following example loads books.xml and outputs the XML tree to the console.

This example uses the following XML document: Sample XML File: Books (LINQ to XML).

XElement booksFromFile = XElement.Load(@"books.xml");

This code produces the following output:

  <Book id="bk101">
    <Author>Garghentini, Davide</Author>
    <Title>XML Developer's Guide</Title>
    <Description>An in-depth look at creating applications 
      with XML.</Description>
  <Book id="bk102">
    <Author>Garcia, Debra</Author>
    <Title>Midnight Rain</Title>
    <Description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</Description>