Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by doutho · Sep 12, 2013 at 01:32 AM · windows-7windows 8metro

XDocument in windows store apps usage.

Hello there guys. I have some question enquiring the usage of XDocument with windows store apps. I tried to include the appropriate libraries in the unity project. The problem is that i get the following error.

Assets\Scripts\USERDATA\UDHS.cs(159,5): error CS0433: The type 'System.Xml.Linq.XDocument' exists in both 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Xml.XDocument.dll' and ...\Assets\System.Xml.Linq.dll'

When i try to solve this by even removing the libraries from the project i keep getting some errors for the strings on the xdocument not being a stream. How can i use a stream instead of string?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DigitalDok · Sep 12, 2013 at 02:57 AM

I am facing the same problem, i really can't find ANY way to properly export my Unity game to Windows Store. I am using XDocument to save the xml (i have Xml.Linq right in my project folder in order to use the XDocument with ease. Everything seems fine with PC version...

I really haven't seen a single tutorial online for this, and many people have the same problem. If you have any Unity3D solution which is using XDocument and XML in order to save stuff or any source code, please provide a snippet! :/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by CanisLupus · Jun 23, 2014 at 11:32 AM

On Windows Store apps (Metro apps), method signatures that received a full path to a file/folder were discarded, as Metro apps can alter only specific directories, exclusive to the application. For this reason, XDocument.Save cannot receive a string for a path anymore, and must instead receive a Stream. You must open a stream to the target file (the one where you want to write) using the new API, and then call Save with that file stream as argument.

The problem with the new API is that it requires the use of complicated async calls, and that eventually one method must wait for the result of the async "get write stream" operation (in this case), as the rest of the Unity game is not async (unless you want to convert large portions of code to use the async capabilites in Windows Metro only).

This example writes an XDocument to a file, using the new API (it also shows the old code to Save an XDocument with the full string path).

 private void someMethodThatSavesXDocument()
 {
     XDocument doc = new XDocument(
         new XElement("example",
             new XElement("name", "John"),
             new XElement("language", "en")
         )
     );
     
     #if !NETFX_CORE
         // active when NOT building for Windows Store apps
         string fullPath = Application.persistentDataPath + "/folder/file.txt";
         doc.Save(fullPath);
     #else
         // active when building for Windows Store apps
         using (Stream stream = GetWriteStream("folder/file.txt"))
         {
             doc.Save(stream);
         }
     #endif
 }

 #if NETFX_CORE
 // calls and waits for the completion of the method that uses the new async API
 public static Stream GetWriteStream(string relativePath)
 {
     // In Unity games for the Windows Store, Application.persistentDataPath
     // points to the same place as this ApplicationData.Current.LocalFolder
     return getWriteStream(ApplicationData.Current.LocalFolder, relativePath).Result;
 }

 // async method that navigates to the file and opens it in write mode
 private static async Task<Stream> getWriteStream(StorageFolder folder, string relativePath)
 {
     string name = Path.GetFileName(relativePath);
     string dir  = Path.GetDirectoryName(relativePath);

     // get intermediate folders to file
     string[] dirNames = dir.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);

     // navigate to last folder in relative path
     foreach (string dirName in dirNames) {
         folder = await folder.GetFolderAsync(dirName);
     }

     // create file and open stream to it
     StorageFile file = await folder.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting);
     return await file.OpenStreamForWriteAsync();
 }
 #endif


Necessary includes for the new API in this example:

 #if NETFX_CORE
 using System;
 using System.Threading.Tasks;
 using Windows.Storage;
 #endif

Please note that this example is only based on existing code of mine, but I haven't tested it as it is. If you find errors please notify me. Also, you should avoid having those duplicated classes that you describe in the question by removing the unnecessary dlls.

Shameless plug: I also created a plugin to abstract the new API regarding IO in the Application.persistentDataPath folder, With it, you can simply call a method for some File/Directory operation, and it handles the new API internally. See the forum post for the asset here and the Asset Store link here.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Winrt] Hashtable & Arraylist compilation error 1 Answer

Handling the lack of System.IO classes in Windows Store Apps (Windows 8 Metro apps) 2 Answers

Windows store apps log file location 1 Answer

Virtual keyboard in Windows Store Apps (Metro) via the TouchScreenKeyboard class 0 Answers

What could i do??? RE-install Windows 7? Fix some drivers? What can i do to fix this??? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges