M
MahmudX
Guest
Hi!
In my UWP app, I need to read from an existing .json file, convert that JSON data to a List<T>, add some new data to that List<T>, convert to that list to JSON data and write that data to that same existing .json file.
How can I do that?
Here is some code that I have written.
private async void saveButton_Click(object sender, RoutedEventArgs e)
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile samplefile = await storageFolder.CreateFileAsync
("SavedData.json", CreationCollisionOption.ReplaceExisting);
StorageFile savedFile = await storageFolder.GetFileAsync("SavedData.json");
string text = await FileIO.ReadTextAsync(savedFile);
var serializer = new DataContractJsonSerializer(typeof(DataFormat));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(text));
List<DataFormat> data = (List<DataFormat>)serializer.ReadObject(ms);
if (data == null)
{
data = new List<DataFormat>();
}
data.Add(new DataFormat
{
Title = fileText.Text,
Director = fileText1.Text,
Rated = fileText2.Text
});
Stream mystream = await savedFile.OpenStreamForWriteAsync();
serializer.WriteObject(mystream, data);
}
DataFormat Class-
public class DataFormat : IEquatable<DataFormat>
{
[DataMember]
public string firstName { get; set; }
[DataMember]
public string lastName { get; set; }
[DataMember]
public string Country { get; set; }
public bool Equals(DataFormat other)
{
string fullName = other.firstName + other.lastName;
if (other == null)
{
return false;
}
return (fullName.Equals(other.fullName));
}
}
When I run this program, this exception appears -
System.Runtime.Serialization.SerializationException was unhandled by user code
HResult=-2146233088
Message=Expecting element 'root' from namespace ''.. Encountered 'None' with name '', namespace ''.
Source=System.Private.DataContractSerialization
StackTrace:
at System.Runtime.Serialization.Json.DataContractJsonSerializerImpl.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
at System.Runtime.Serialization.XmlObjectSerializer.InternalReadObject(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.Json.DataContractJsonSerializerImpl.ReadObject(XmlDictionaryReader reader)
at System.Runtime.Serialization.Json.DataContractJsonSerializerImpl.ReadObject(Stream stream)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(Stream stream)
at UWP_TestingGround.MainPage.<saveButton_Click>d__2.MoveNext()
InnerException:
[Note: I am new in C# so detailed information will be helpful.]
Continue reading...
In my UWP app, I need to read from an existing .json file, convert that JSON data to a List<T>, add some new data to that List<T>, convert to that list to JSON data and write that data to that same existing .json file.
How can I do that?
Here is some code that I have written.
private async void saveButton_Click(object sender, RoutedEventArgs e)
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile samplefile = await storageFolder.CreateFileAsync
("SavedData.json", CreationCollisionOption.ReplaceExisting);
StorageFile savedFile = await storageFolder.GetFileAsync("SavedData.json");
string text = await FileIO.ReadTextAsync(savedFile);
var serializer = new DataContractJsonSerializer(typeof(DataFormat));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(text));
List<DataFormat> data = (List<DataFormat>)serializer.ReadObject(ms);
if (data == null)
{
data = new List<DataFormat>();
}
data.Add(new DataFormat
{
Title = fileText.Text,
Director = fileText1.Text,
Rated = fileText2.Text
});
Stream mystream = await savedFile.OpenStreamForWriteAsync();
serializer.WriteObject(mystream, data);
}
DataFormat Class-
public class DataFormat : IEquatable<DataFormat>
{
[DataMember]
public string firstName { get; set; }
[DataMember]
public string lastName { get; set; }
[DataMember]
public string Country { get; set; }
public bool Equals(DataFormat other)
{
string fullName = other.firstName + other.lastName;
if (other == null)
{
return false;
}
return (fullName.Equals(other.fullName));
}
}
When I run this program, this exception appears -
System.Runtime.Serialization.SerializationException was unhandled by user code
HResult=-2146233088
Message=Expecting element 'root' from namespace ''.. Encountered 'None' with name '', namespace ''.
Source=System.Private.DataContractSerialization
StackTrace:
at System.Runtime.Serialization.Json.DataContractJsonSerializerImpl.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
at System.Runtime.Serialization.XmlObjectSerializer.InternalReadObject(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.Json.DataContractJsonSerializerImpl.ReadObject(XmlDictionaryReader reader)
at System.Runtime.Serialization.Json.DataContractJsonSerializerImpl.ReadObject(Stream stream)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(Stream stream)
at UWP_TestingGround.MainPage.<saveButton_Click>d__2.MoveNext()
InnerException:
[Note: I am new in C# so detailed information will be helpful.]
Continue reading...