• windows phone8.1:Xml,Json序列化和反序列化


    小梦本例主要实现以下四点内容:

    1. 将Car对象序列化为xml
    2. 将Car对象序列化为Json
    3. 将xml反序列化为Car对象
    4. 将json反序列化为json对象

    Car类如下:

    [task]public class Car
    {
    public int Id { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
    }[/task]

    实例化Car对象:

    [task]private List<Car> buildObjectGraph()
    {
    var myCars = new List<Car>();

    myCars.Add(new Car() { Id = 1, Make = "Oldsmobile", Model = "Cutlas Supreme", Year = 1985 });
    myCars.Add(new Car() { Id = 2, Make = "Geo", Model = "Prism", Year = 1993 });
    myCars.Add(new Car() { Id = 3, Make = "Ford", Model = "Escape", Year = 2005 });

    return myCars;
    }[/task]

    将Car对象序列化为xml:

    [task]

    private async Task writeXMLAsync()
    {
    var myCars = buildObjectGraph();

    var serializer = new DataContractSerializer(typeof(List<Car>));
    using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
    XMLFILENAME,
    CreationCollisionOption.ReplaceExisting))
    {
    serializer.WriteObject(stream, myCars);
    }

    resultTextBlock.Text = "Write succeeded";
    }

    [/task]

    将Car对象序列化为Json:

    [task]

    private async Task readJsonAsync()
    {
    // Notice that the write **IS** identical ... except for the serializer.

    string content = String.Empty;

    var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(JSONFILENAME);
    using (StreamReader reader = new StreamReader(myStream))
    {
    content = await reader.ReadToEndAsync();
    }

    resultTextBlock.Text = content;
    }

    [/task]

    将xml反序列化为Car对象:

    [task]private async Task deserializeXmlAsync()
    {
    string content = String.Empty;

    List<Car> myCars;
    var xmlSerializer = new DataContractSerializer(typeof(List<Car>));
    var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(XMLFILENAME);
    myCars = (List<Car>)xmlSerializer.ReadObject(myStream);
    foreach (var car in myCars)
    {
    content += String.Format("ID: {0}, Make: {1}, Model: {2} ", car.Id, car.Make, car.Model);
    }

    resultTextBlock.Text = content;

    }[/task]

    将json反序列化为json对象

    [task]private async Task deserializeJsonAsync()
    {
    string content = String.Empty;

    List<Car> myCars;
    var jsonSerializer = new DataContractJsonSerializer(typeof(List<Car>));

    var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(JSONFILENAME);

    myCars = (List<Car>)jsonSerializer.ReadObject(myStream);

    foreach (var car in myCars)
    {
    content += String.Format("ID: {0}, Make: {1}, Model: {2} ", car.Id, car.Make, car.Model);
    }

    resultTextBlock.Text = content;
    }[/task]

    欢迎光临编程小梦。

  • 相关阅读:
    介绍一款jquery ui组件gijgo(含tree树状结构、grid表格),特点:简易、文档全清晰易懂、示例代码
    【未完待续】API接口
    表单中Readonly和Disabled的区别:readonly在get和post可传值到后端,disabled不可
    Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案
    一种历史详细记录表,完整实现:CommonOperateLog 详细记录某用户、某时间、对某表、某主键、某字段的修改(新旧值
    js return falsee.preventDefault() 以及session
    bootstrape学习
    Redis的PHP操作手册
    PHP表单常用正则表达式(URL、HTTP、手机、邮箱等)
    大型网站架构演化
  • 原文地址:https://www.cnblogs.com/bcmeng/p/3780705.html
Copyright © 2020-2023  润新知