HOW TO:從檔案載入 XML
這個主題顯示如何使用 XElement.Load 方法,從 URI 載入 XML。
範例
下列範例顯示如何從檔案載入 XML 文件。 下列範例會載入 books.xml,並將 XML 樹狀結構輸出到主控台。
此範例使用下列 XML 文件:XML 範例檔:書籍 (LINQ to XML)。
XElement booksFromFile = XElement.Load(@"books.xml");
Console.WriteLine(booksFromFile);
Dim booksFromFile As XElement = XElement.Load("books.xml")
Console.WriteLine(booksFromFile)
此程式碼會產生下列輸出:
<Catalog>
<Book id="bk101">
<Author>Garghentini, Davide</Author>
<Title>XML Developer's Guide</Title>
<Genre>Computer</Genre>
<Price>44.95</Price>
<PublishDate>2000-10-01</PublishDate>
<Description>An in-depth look at creating applications
with XML.</Description>
</Book>
<Book id="bk102">
<Author>Garcia, Debra</Author>
<Title>Midnight Rain</Title>
<Genre>Fantasy</Genre>
<Price>5.95</Price>
<PublishDate>2000-12-16</PublishDate>
<Description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</Description>
</Book>
</Catalog>