XDocument.Load XML檔案
假設在專案中加入一個menu2.xml的檔案﹐並如下設定建置動作與複製到輸出目錄
那麼Load檔案的方式則如下
XDocument doc = XDocument.Load("/專案名稱;component/Menu2.xml");
如果xml檔案是由程式產生放置於Isolated Storage的話﹐那麼就必須使用Isolated Storage的方法讀出檔案
string txt = "";
txt += "<menu>";
txt += " <item code=\"10\" name=\"選項一\"/>";
txt += " <item code=\"20\" name=\"選項二\"/>";
txt += " <item code=\"30\" name=\"選項三\"/>";
txt += "</menu>";
IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();
if (!isoFile.FileExists("/Menu.xml")) {
StreamWriter sw = new StreamWriter(isoFile.CreateFile("/Menu.xml"), System.Text.Encoding.UTF8);
sw.Write(txt);
sw.Close();
sw.Dispose();
}
isoFile.Dispose();
......................................
......................................
IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();
using (isoFile) {
XDocument doc = XDocument.Load(isoFile.OpenFile("/Menu.xml", FileMode.Open));
var query = from q in doc.Descendants("item")
select q;
listBox1.ItemsSource = query;
}