简体   繁体   English

无法加载文件或程序集System.Runtime.Serialization.Xml

[英]Could not load file or assembly System.Runtime.Serialization.Xml

I'm developing a UWP app. 我正在开发UWP应用。 The solution contains a number of projects written in C# (such as the database), but the UI is a JavaScript project. 该解决方案包含许多用C#编写的项目(例如数据库),但是UI是JavaScript项目。

One of the C# classes contains the following code to save a copy of the database using DataContractSerializer: C#类之一包含以下代码,以使用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: 当此代码从JS运行时,出现以下错误:

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. 我最终删除了所有对数据协定的引用,并用自定义函数替换了它以构建JsonObject。 I then write that to a file myself rather than relying on the DataContractSerializer. 然后,我自己将其写入文件,而不是依赖于DataContractSerializer。

Each class within the database now has a ToJson function, similar to: 现在数据库中的每个类都有一个ToJson函数,类似于:

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: 每个类还包含一个接受Json对象的构造函数,例如:

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. 所有这些都比弄乱数据合同要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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