簡體   English   中英

無法加載文件或程序集System.Runtime.Serialization.Xml

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

我正在開發UWP應用。 該解決方案包含許多用C#編寫的項目(例如數據庫),但是UI是JavaScript項目。

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;
    }
}

當此代碼從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.

如果有人能向我指出正確的方向,我正在努力尋找在線解決方案?

謝謝。

我最終刪除了所有對數據協定的引用,並用自定義函數替換了它以構建JsonObject。 然后,我自己將其寫入文件,而不是依賴於DataContractSerializer。

現在數據庫中的每個類都有一個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;
}

每個類還包含一個接受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);
}

然后可以使用以下命令將其寫入文件:

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;
    }
}

並使用以下內容閱讀:

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;
    }
}

所有這些都比弄亂數據合同要容易得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM