Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法加载文件或程序集System.Runtime.Serialization.Xml_Javascript_C#_Windows 10 Universal - Fatal编程技术网

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

Javascript 无法加载文件或程序集System.Runtime.Serialization.Xml,javascript,c#,windows-10-universal,Javascript,C#,Windows 10 Universal,我正在开发一个UWP应用程序。该解决方案包含许多用C#编写的项目(如数据库),但UI是一个JavaScript项目 其中一个C#类包含使用DataContractSerializer保存数据库副本的以下代码: public IAsyncOperation<Boolean> Save() { return Save(0).AsAsyncOperation<Boolean>(); } private async Task<Boolean> Save(In

我正在开发一个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;
    }
}
如果有人能给我指出正确的方向,我正努力在网上找到解决方案


谢谢。

我最终删除了对数据契约的所有引用,并用自定义函数替换它以构建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 async Task Save()
{
尝试
{
updated=DateTime.Now;
StorageFile file=wait ApplicationData.Current.RoamingFolder.CreateFileAsync(id+“.json”,CreationCollisionOption.ReplaceExisting);
等待FileIO.WriteTextAsync(文件,ToJson().Stringify());
返回true;
}
捕获(例外)
{
返回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;
    }
}
私有静态异步任务打开(字符串id)
{
尝试
{
StorageFile file=wait ApplicationData.Current.RoamingFolder.GetFileAsync(id+“.json”);
返回新数据库(JsonObject.Parse(wait FileIO.ReadTextAsync(file));
}
捕获(例外)
{
返回null;
}
}

所有这些都比摆弄数据契约容易得多。

您使用的是Nuget软件包吗?不,不使用任何Nuget(或者,就此而言,任何第三方)软件包。
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;
    }
}