ScottS
ScottS

Reputation: 79

Unable to read XML file

I am new to UWP and can't get my XAML page to read an XML file. I want to take the entries in the XML file and load them into a dropdown. I have not set the dropdown up in my XAML page yet, but am just trying to read in an XML file. When I get to the C# method (Page_Loaded) it gets an error saying I can't do a .Load command in the UI thread and it needs to be moved to Task.Run. Here is my XAML...

<Page
    x:Class="TestProject.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestProject"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Loaded="Page_Loaded">
</Page>  

Here is my code behind...

using System.Xml.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace TestProject
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            XDocument file = XDocument.Load(@"C:\AvailableTestsXMLFile.txt");             

        }       
    }
}

// Any help would be appreciated!

// here is the exact error...Synchronous operations should not be performed on the UI thread. Consider wrapping this method in Task.Run.

Upvotes: 0

Views: 844

Answers (1)

thezapper
thezapper

Reputation: 496

  1. you could check this site: UWP XmlDocument

  2. In UWP you can't access files accept from your application folder without filepicker. In a future Update you'll get some more possibilities, but for now - no

  3. Try to make a method:

    public async Task LoadXML(string file)
    {
       await Task.Run(() => { XDocument xml = XDocument.Load(file); });
    }
    

Upvotes: 2

Related Questions