Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

Save and Load app data with XML file error -

cre8iveparkcre8ivepark ✭✭
edited October 2016 in Questions And Answers

Hi, I am trying to save and load scene data using XML. Following this tutorial, I was able to save and load in the Unity editor (game mode).
https://youtu.be/Y8Di-Q6qpU4

However, in device or emulator, I am getting this error:

UnauthorizedAccessException: Access to the path .......\Data\Resources\actors.xml' is denied.

at System.IO.Win32FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs)
at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
at System.IO.MultiplexingWin32WinRTFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at SaveData.SaveActors(String path, ActorContainer actors)
at SaveData.Save(String path, ActorContainer actors)
at HoloToolkit.Unity.ButtonEvent.OnSelect()
at HoloToolkit.Unity.ButtonEvent.$Invoke5OnSelect(Int64 instance, Int64* args)
at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)

Here is the code that saves and loads XML file. According to initial investigation, it looks like it is related to UWP's access limitation to the local files/folders. The xml file is under Data/Resources/ folder. Any suggestions would be appreciated! Thanks!

private static ActorContainer LoadActors(string path)
{
    XmlSerializer serializer = new XmlSerializer(typeof(ActorContainer));
    FileStream stream = new FileStream(path, FileMode.Open);
    ActorContainer actors = serializer.Deserialize(stream) as ActorContainer;
    stream.Dispose();

    return actors;
}

private static void SaveActors(string path, ActorContainer actors)
{
    XmlSerializer serializer = new XmlSerializer(typeof(ActorContainer));
    FileStream stream = new FileStream(path, FileMode.Truncate);
    serializer.Serialize(stream, actors);

    stream.Dispose();
}

Best Answer

Answers

  • Options

    @cre8ivepark
    Here is a sample of saving and reading files with the UWP syntax in Unity in case it helps.

    Windows Holographic User Group Redmond

    WinHUGR.org - - - - - - - - - - - - - - - - - - @WinHUGR
    WinHUGR YouTube Channel -- live streamed meetings

  • Options
    cre8iveparkcre8ivepark ✭✭
    edited October 2016

    @HoloSheep Thank you so much!
    I found similar code in HoloToolkit's MeshSaver.cs and replaced FileStream related code with your suggestion. Now both saving and loading are working when I try for the first time but it gives me these errors (both for saving and loading). It only works first time when I close the app and fresh start. Then it generates these errors. I have no clue... Not sure if it is related to stream.Dispose() or Flush()
    Thank you!

    ******************** Load *********************************

    (Filename: C:\buildslave\unity\build\artifacts/generated/Metro/runtime/UnityEngineDebugBindings.gen.cpp Line: 45)

    ArgumentNullException: Value cannot be null.
    Parameter name: input
    at System.Xml.XmlReaderSettings.CreateReader(Stream input, Uri baseUri, String baseUriString, XmlParserContext inputContext)
    at System.Xml.XmlReader.Create(Stream input, XmlReaderSettings settings, String baseUri)
    at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
    at SaveData.LoadActors(String path)
    at SaveData.Load(String path)
    at HoloToolkit.Unity.ButtonEvent.OnSelect()
    at HoloToolkit.Unity.ButtonEvent.$Invoke5OnSelect(Int64 instance, Int64* args)
    at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
    (Filename: Line: 0)

    ******************** Save *********************************
    ArgumentNullException: Value cannot be null.
    Parameter name: output
    at System.Xml.XmlWriterSettings.CreateWriter(Stream output)
    at System.Xml.XmlWriter.Create(Stream output, XmlWriterSettings settings)
    at System.Xml.Serialization.XmlSerializer.Serialize(Stream stream, Object o, XmlSerializerNamespaces namespaces)
    at SaveData.SaveActors(String path, ActorContainer actors)
    at SaveData.Save(String path, ActorContainer actors)
    at HoloToolkit.Unity.ButtonEvent.OnSelect()
    at HoloToolkit.Unity.ButtonEvent.$Invoke5(Int64 instance, Int64* args)
    at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
    (Filename: Line: 0)

    Code ------------------------------------------------------------

    private static ActorContainer LoadActors(string path)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(ActorContainer));
    
    
        Stream stream = null;
    
            Task task = new Task(
                            async () =>
                            {
                                StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folderName);
                                StorageFile file = await folder.GetFileAsync(fileName);
    
                                //StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Data/Resources/textObjectData.xml"));
                                stream = await file.OpenStreamForReadAsync();
                            });
            task.Start();
            task.Wait();
    
    
        ActorContainer actors = serializer.Deserialize(stream) as ActorContainer;
        //stream.Dispose();
    
        return actors;
    }
    
    private static void SaveActors(string path, ActorContainer actors)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(ActorContainer));
    
        Stream stream = null;
    
    
    
            Task task = new Task(
                            async () =>
                            {
                                StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folderName);
                                StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
                                stream = await file.OpenStreamForWriteAsync();
                            });
            task.Start();
            task.Wait();
    
        serializer.Serialize(stream, actors);
    
        //stream.Dispose();
        stream.Flush();
    
    }
    
  • Options
    cre8iveparkcre8ivepark ✭✭
    edited November 2016

    Thank you very much @HoloSheep I was able to remove those errors but I spent few days with this different error. It looks like it is related to transform but when I check it is not null. I couldn't find this keyword Component_Get_Custom_PropTransform from Unity forum.

    NullReferenceException: Exception of type 'System.NullReferenceException' was thrown.
    at WinRTBridge.Utils.ThrowNewNullReferenceException(String message)
    at UnityEngineProxy.InternalCalls.PInvokeCalls.Component_Get_Custom_PropTransform(IntPtr param_0)
    at UnityEngineProxy.InternalCalls.Component_Get_Custom_PropTransform(Object self)
    at Actor.StoreData()
    at Actor.b__8_1()
    at SaveData.SerializeAction.Invoke()
    at SaveData.Save(String path, ActorContainer actors)
    at HoloToolkit.Unity.ButtonEvent.OnSelect()
    at HoloToolkit.Unity.ButtonEvent.$Invoke5OnSelect(Int64 instance, Int64* args)
    at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)

    Related code is:

        Vector3 pos = transform.position;
        data.posX = pos.x;
        data.posY = pos.y;
        data.posZ = pos.z;
    

    This script is included in the GameObject, which is instantiated from a prefab resource. Not sure how transform could be null.

  • Options

    Resolved the issue and now XML save and load is working. Thank you @HoloSheep for your suggestion!

Sign In or Register to comment.