Crossplatform, PCL data loading

In PCL library there is easy way to load Assets (Content.Load<> method), however there is no way to load Xml or other configs. How do You solve this problem?

In other topics (especially connected to xmls) people suggest using File.Open method, however it is not available in PCL and/or on some platforms.

There are 2 possible solutions.

You can load XML file (or JSON file or whatever) with Content Pipeline (as long as it can read and parse that file), then simply load that file a asset via Content.Load<T>() method.

Second is to install PCLStorage NuGet package, which provides interfaces to for all basic file operations.

Maybe I did not stated it clearly enough, but I would like to load things that are not processed by content pipeline.

I’ll take look for PCLStorage, nice hint!

You can also add a file to the project as resource(by setting it’s Build Action to ‘Embedded Resource’).
And then load with the following code:

private static string GetStringResource()
{
    var assembly = typeof(AnyClassOfYourAssembly).GetTypeInfo().Assembly;

    string s;
    using (var stream = assembly.GetManifestResourceStream("Class.Path.To.Resource"))
    {
        using (var reader = new StreamReader(stream))
        {
            s = reader.ReadToEnd();
        }
    }
    return s;
}

“Class.Path.To.Resource” is the full class path to the resource file. I.e. if your assembly name is “MyAssembly” and the resource file “file.xml” had been added under folder “Resources”. Then it’s class path will be “MyAssembly.Resources.file.xml”.
If you have problem with method GetTypeInfo, make sure namespace System.Reflection is used.
This way works both with PCL and ordinary assemblies.

Thanks for Your hints.

I’ve also found that there is built-in way of doing that, by using TitleContainer (I do not know who named it like that). You can give it path relative to application root and it will return Stream to file. Compared to Content.Load<> it needs content root path prefixed.