简体   繁体   中英

Could not load file or assembly System.Runtime.Serialization.Xml

I'm developing a UWP app. The solution contains a number of projects written in C# (such as the database), but the UI is a JavaScript project.

One of the C# classes contains the following code to save a copy of the database using DataContractSerializer:

public IAsyncOperation<Boolean> Save()
{
    return Save(0).AsAsyncOperation<Boolean>();
}

private async Task<Boolean> Save(Int32 notUsed)
{
    try
    {
        updated = DateTime.Now;

        StorageFile file = await ApplicationData.Current.RoamingFolder.CreateFileAsync(id + ".xml", CreationCollisionOption.ReplaceExisting);
        IRandomAccessStream access = await file.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.AllowOnlyReaders);
        Stream stream = access.AsStreamForWrite();

        DataContractSerializer serializer = new DataContractSerializer(typeof(Database));
        serializer.WriteObject(stream, this);

        stream.Dispose();
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

When this code gets run from JS, I get the following error:

0x80070002 - JavaScript runtime error: The system cannot find the file specified.
System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.Serialization.Xml, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
WinRT information: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.Serialization.Xml, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

I'm struggling to find a solution online if anyone is able to point me in the right direction?

Thanks.

I ended up removing all references to the data contracts, and replacing it with custom functions to build a JsonObject. I then write that to a file myself rather than relying on the DataContractSerializer.

Each class within the database now has a ToJson function, similar to:

internal JsonObject ToJson()
{
    JsonObject root = new JsonObject();
    root.Add(JSON_ID, JsonValue.CreateStringValue(Id));
    root.Add(JSON_DELETED, JsonValue.CreateBooleanValue(Deleted));
    root.Add(JSON_PHOTO, Photo != null ? Photo.ToJson() : null);
    root.Add(JSON_PREFIX, JsonValue.CreateStringValue(Prefix));
    root.Add(JSON_GIVENNAME, JsonValue.CreateStringValue(GivenName));
    root.Add(JSON_MIDDLENAMES, JsonValue.CreateStringValue(MiddleNames));
    root.Add(JSON_FAMILYNAME, JsonValue.CreateStringValue(FamilyName));
    root.Add(JSON_SUFFIX, JsonValue.CreateStringValue(Suffix));
    root.Add(JSON_NOTES, JsonValue.CreateStringValue(Notes));
    return root;
}

And each class also contains a constructor that accepts a Json object, such as:

internal Person(JsonObject root) : this()
{
    Id = root.GetNamedString(JSON_ID, null);
    Deleted = root.GetNamedBoolean(JSON_DELETED, false);
    Photo = root.GetNamedObject(JSON_PHOTO, null) != null ? new Photo(root.GetNamedObject(JSON_PHOTO, null)) : null;
    Prefix = root.GetNamedString(JSON_PREFIX, null);
    GivenName = root.GetNamedString(JSON_GIVENNAME, null);
    MiddleNames = root.GetNamedString(JSON_MIDDLENAMES, null);
    FamilyName = root.GetNamedString(JSON_FAMILYNAME, null);
    Suffix = root.GetNamedString(JSON_SUFFIX, null);
    Notes = root.GetNamedString(JSON_NOTES, null);
}

I can then write it to a file using:

private async Task<Boolean> Save()
{
    try
    {
        updated = DateTime.Now;

        StorageFile file = await ApplicationData.Current.RoamingFolder.CreateFileAsync(id + ".json", CreationCollisionOption.ReplaceExisting);
        await FileIO.WriteTextAsync(file, ToJson().Stringify());
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

And read it using:

private static async Task<Database> Open(String id)
{
    try
    {
        StorageFile file = await ApplicationData.Current.RoamingFolder.GetFileAsync(id + ".json");
        return new Database(JsonObject.Parse(await FileIO.ReadTextAsync(file)));
    }
    catch (Exception)
    {
        return null;
    }
}

All of which was a LOT easier than messing around with data contracts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM