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?

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! :confused:

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")
		// active when NOT building for Windows Store apps
		string fullPath = Application.persistentDataPath + "/folder/file.txt";
		// active when building for Windows Store apps
		using (Stream stream = GetWriteStream("folder/file.txt"))

// 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();

Necessary includes for the new API in this example:

using System;
using System.Threading.Tasks;
using Windows.Storage;

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.