Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a packaged file with WinRT

I am trying to figure out how to port some .Net code that parsed an xml file to WinRT. So far, with the help of The given System.Uri cannot be converted into a Windows.Foundation.Uri, I have the below code. Still, I get stuck immediately after I create the Uri:

    static readonly Uri ResourcesBase = new Uri(@"ms-resource://MyAssembly/"); 
    public override async void Load()
    {
        Uri uri = new Uri(ResourcesBase, filePath); // filePath = "Data//world.xml";
        XmlLoadSettings settings = new XmlLoadSettings() { ValidateOnParse = false };

        XmlDocument xmlDoc = await XmlDocument.LoadFromUriAsync(uri, settings);

        foreach (IXmlNode xmlNode in xmlDoc.ChildNodes)
        {
            ProcessNode(xmlNode);
        }
    }

I get an unhandled exception when I try to call XmlDocument.LoadFromUriAsyn(uri):

ArgumentException was unhandled by the user code - Value does not fall within the expected range.

Anyone else feel like everything is 10 times harder with WinRT?

EDIT:

I have tried all the following strings, and get the exact same error:

  Uri uri = new Uri("ms-resource://MyAssembly//" + filePath);
  Uri uri = new Uri("ms-resource://MyAssembly/" + filePath);
  Uri uri = new Uri("d:\\projects\\crystal\\" + filePath); // A valid absolute path

Project Set Up:

Project

  • Properties
  • References
  • Assets
  • Data
    • world.xml
  • Source code...

In Code:

  filePath = "Data\\world.xml";

I have also tried putting the xml file under assset\data, and just assets. Nothing seems to make a difference.

Another thing, I have the Build Action of the xml set to "Content". Is that correct? The only other thing I could imagine that it would be is "Embedded Resource" but I doubt it.

Full Exception details:

System.ArgumentException was unhandled by user code

HResult=-2147024809

Message=Value does not fall within the expected range.

Source=Windows.Data.Xml.Dom

StackTrace:

at Windows.Data.Xml.Dom.XmlDocument.LoadFromUriAsync(Uri uri, XmlLoadSettings loadSettings)

at Crystal.IO.File.XmlFileSerializer.d__1.MoveNext() in d:\Projects\Crystal\library\IO\File\XmlFileSerializer.cs:line 32

InnerException:

Download the smallest example possible to repro the issue: test_xml.zip

like image 968
Nick Banks Avatar asked Mar 04 '12 04:03

Nick Banks


Video Answer


1 Answers

I finally figured it out after I looked at Windows Runtime Xml data API sample.

    public override async Load()
    {
        var file = await GetPackagedFile("assets", "world.xml");
        LoadXml(file);
    }

    private async void LoadXml(StorageFile file)
    {
        XmlLoadSettings settings = new XmlLoadSettings() { ValidateOnParse = false };
        XmlDocument xmlDoc = await XmlDocument.LoadFromFileAsync(file, settings);

        foreach (IXmlNode xmlNode in xmlDoc.ChildNodes)
        {
            //ProcessNode(xmlNode);
        }
    }

    private async Task<StorageFile> GetPackagedFile(string folderName, string fileName)
    {
        StorageFolder installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;

        if (folderName != null)
        {
            StorageFolder subFolder = await installFolder.GetFolderAsync(folderName);
            return await subFolder.GetFileAsync(fileName);
        }
        else
        {
            return await installFolder.GetFileAsync(fileName);
        }
    }
}
like image 124
Nick Banks Avatar answered Sep 28 '22 11:09

Nick Banks