How to open a packaged file with WinRT

前端 未结 1 897
情书的邮戳
情书的邮戳 2021-02-20 01:59

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.Foundatio

1条回答
  •  北海茫月
    2021-02-20 02:23

    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 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);
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题