您的位置:首页 > 其它

windowsphone8.1学习笔记之应用数据(四)

2015-08-22 23:46 369 查看
应用数据的存储格式常用的分为json和xml两种(其实我都想略过这个地方的,json我一直用的是json.net,而wp上操作xml的方式与其他相比也没太多变化)。

先说说json数据存储,关于json概念就不粘贴复制了。在wp里使用json数据格式来存储相关的信息会有两种编程方式:DataContractJsonSerializer类和JsonObject。

DataContractJsonSerializer类对Json数据进行序列化和反序列化是最简洁的Json数据操作方式,序列化的过程是把实体类对象转化为json字符串对象,该操作是直接把实体类的属性名称和属性值组成“名称/值”的形式,反序列化的过程则刚好倒过来。

public string DataToJson(object obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
string result = string.Empty;
using(MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, obj);
ms.Position = 0;
using(StreamReader reader = new StreamReader(ms))
{
result = reader.ReadToEnd();
}
}

return result;
}

public T JsonToData<T>(string jsonString)
{
var ds = new DataContractJsonSerializer(typeof(T));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ds.ReadObject(ms);
ms.Dispose();
return obj;
}


以上就是DataContractJsonSerializer类序列化和反序列化的方法,接下来说说JsonObject类自定义json对象。

//组装json串
//jsonObject.SetNameString("Name",JsonValue.CreateStringValue("waha"))
public string GetJsonByJsonObject()
{
JsonObject jsonObject = new JsonObject();
jsonObject["Name"] = JsonValue.CreateStringValue("waha");
jsonObject["Age"] = JsonValue.CreateNumberValue(11);
return jsonObject.Stringify();
}

//读取json
JsonObject jsonObject = JsonObject.Parse(jsonString);
//第二参数为默认值
var name = jsonObject.GetNamedString("Name","");
var age = jsonObject.GetNamedNumber("Name",0);


以上就是json序列化和反序列化,接下来简单说说xml的操作,DataContractSerializer类和XmlDocument类(其实我用的较多的是XDocument)。先说说DataContractSerializer类,这个的名称和上面的比较相似,就是少了个“Json”。

public async void ObjectToXml<T>(T data,string fileName)
{
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
using (IOutputStream outStream = stream.GetOutputStreamAt(0))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(outStream.AsStreamForWrite(), data);
await outStream.FlushAsync();
}
}

public async Task<T> XmlToObject<T>(string filename)
{
T _sessionState = default(T);
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
if (file == null) return _sessionState;
using (IInputStream inStream = await file.OpenSequentialReadAsync())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
_sessionState = (T)serializer.ReadObject(inStream.AsStreamForRead());
}
return _sessionState;
}


测试如下:

People p = new People { Name = "waha", Age = 11 };
ObjectToXml<People>(p, "testSerializer");
var file = await ApplicationData.Current.LocalFolder.GetFileAsync("testSerializer");
var str = await FileIO.ReadTextAsync(file);


str的值如下:

<People xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/App1"><Age>11</Age><Name>waha</Name></People>


应用数据的存储格式就说到这里,整个应用数据的内容也就完了,晚安!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: