簡體   English   中英

如何在 UWP C# 中的 XDocument.Load 方法中給出 xml 文件路徑

[英]How to give xml file path in XDocument.Load method in UWP C#

我在 Assets/Fonts/FontAwsome_version5.xml 有一個 XML 文件,我嘗試使用以下方法讀取此文件,但出現錯誤 - uri 前綴無法識別

XDocument xmlDocument = XDocument.Load(filePath); string filePath = "ms-appx:///Assets/Fonts/FontAwsome_version5.xml";

如何在 UWP C# 中的 XDocument.Load 方法中給出 xml 文件路徑

I'm afraid you can't load the file with UWP uri scheme for System.Xml.Linq namespace api, for the requirement, you could use Windows Storage Api to open the file as stream then call XDocument load stream method to access the file's xml 內容。 對於測試,以下代碼可以直接工作。

private async void Button_Click(object sender, RoutedEventArgs e)
{
   string filePath = "ms-appx:///Assets/Font.xml";
   var file =  await StorageFile.GetFileFromApplicationUriAsync(new Uri(filePath));
    using (var stream = await file.OpenStreamForReadAsync())
    {
        XDocument xmlDocument = XDocument.Load(stream);

    }
  
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM