Search code examples
c#uwpwin-universal-appwindows-10-universaluwp-xaml

Connect to local xml File and create button from results


I have a xml file name Albums situated in my Assets folder of my UWP

I then have a SplitView with a VariableSizedWrapGrid attached that I would get information from the XML file and create a button for each item accordingly. Such as Image Source/Title/Year.

Any examples would be very welcome, as I've not done much c# programming in a while.


Solution

  • To parse the xml file, you could use the Windows.Data.Xml.Dom Namespace APIs.

    Here's an official XML DOM sample for your reference. See the scenario 3 & 4.

    In your case, you said that the xml file is in 'Assets' folder. Then, you first need to get the xml file from Windows.ApplicationModel.Package.Current.InstalledLocation. Please note that the files in the location of installed package are read-only, you could only read it.

    After you get the xml file, then you could load it by the relevant APIs.

    var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
    var file = await folder.GetFileAsync("your.xml");
    var doc = await Windows.Data.Xml.Dom.XmlDocument.LoadFromFileAsync(file);