WindowsPhone中XML和Json格式的数据解析
2012-08-31 22:51
423 查看
由于WindowsPhone的沙箱机制,致使关系型数据库无法很好的在WindowsPhone手机中大行其道,此外,大型的数据库一方面打破了WindowsPhone的沙箱机制,破坏了系统的严密、安全性,可能会带来一些意想不到的危害,另一方面数据库服务的开启会一直占用系统的内存。但有时候有些应用却又需要存储大量的数据,这个时候XML的优势就体现的淋漓尽致了,此外一些WebService返回的结果大都也是XML格式或者Json格式的数据,这时候就要用到XML或者Json解析返回的数据,这便是本文的初衷。
先来讲一下XML解析本地的数据,
一、首先应在当前项目下创建一个.xml文件,例如本例的SpokenEnglish.xml文件
文件的格式如下:
<?xml version="1.0" encoding="utf-8" ?>
<SpokenEnglish>
<Items>
<Item>
<Spoken>1. I see.</Spoken>
<Chinese>我明白了。</Chinese>
</Item>
<Item>
<Spoken>2. I quit!</Spoken>
<Chinese>我不干了! </Chinese>
</Item>
<Item>
<Spoken>3. Let go! </Spoken>
<Chinese>放手! </Chinese>
</Item>
<Item>
<Spoken>4. Me too.</Spoken>
<Chinese>我也是。</Chinese>
</Item>
<Item>
<Spoken>5. My god!</Spoken>
<Chinese>天哪!</Chinese>
</Item>
</Items>
</SpokenEnglish>
这是本地的数据存储文件
下边再建一个与源数据相映射的类:SpokenEnglishes.cs
如下:
在使用之前应先加入以下命名空间
using System.Collections.Generic;//对应List<T>泛型集合
using System.Linq;//对应Element.load().Select();
using System.Xml.Linq;//对应XDocument
namespace XMLAndJSON
{
public class SpokenEnglishes
{
public static readonly string xmlPath;//数据文件的路径
public string Spoken
{
get;
set;
}
public string Chinese
{
get;
set;
}
static SpokenEnglishes()
{
xmlPath = @"SpokenEnglish.xml";
}
XML解析方法一
public static List<SpokenEnglishes> GetAllSpokenEnglishes()//获取数据文件中的数据,并映射为SpokenEnglishes类
{
return XElement.Load(xmlPath).Element("Items").Descendants("Item").Select(item => new SpokenEnglishes()
{
Spoken = item.Element("Spoken").Value,
Chinese=item.Element("Chinese").Value
}).ToList();
}
XML解析方法二
public static List<SpokenEnglishes> GetAll()//与上一个函数功能相同,只不过是另一个XML解析方法
{
XDocument xDocument = XDocument.Load("SpokenEnglish.xml");
return xDocument.Descendants("Item").Select(item => new SpokenEnglishes()
{
Chinese = item.Element("Chinese").Value,
Spoken = item.Element("Spoken").Value
}).ToList();
}
}
}
然后就是在MainPage.xaml页进行UI的布局和数据的绑定
主要代码如下:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Height="591" HorizontalAlignment="Left" Margin="10,10,0,0" Name="listBox1" VerticalAlignment="Top"
Width="440" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Name="spoken" Text="{Binding Spoken}"/>//与SpokenEnglish类属性相绑定
<TextBlock Text="{Binding Chinese}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
然后在MainPage.xaml.cs页的构造函数里为listbox1指定数据源
代码如下: listBox1.ItemsSource = SpokenEnglishes.GetAll();//静态类SpokenEnglishes调用其GetAll()方法获取List<SpokenEnglishes>的集合
运行效果如下:
二、用XML解析Web服务返回的数据跟解析本地数据的方法差不多,
大体步骤如下:
1、先根据返回的XML数据格式创建与数据相互映射的实体类:
例如:AirLine类,航班信息类
public class AirLine1
{
public string Company1 { get; set; }
public string AirLineCode1 { get; set; }
public string StartDrome1 { get; set; }
public string EndDrome1 { get; set; }
public string StartTime1 { get; set; }
public string EndTime1 { get; set; }
public string AirLineStop1 { get; set; }
public string Week1 { get; set; }
}
然后在异步返回函数中做出如下的解析:
void airws_getDomesticAirlinesTimeCompleted(object sender, AirLineWS.getDomesticAirlinesTimeCompletedEventArgs e)
{
try
{//关键的解析部分
List<AirLine1> airlinelist = e.Result.Nodes.Descendants("AirlinesTime").Select(
item => new AirLine1()
{
Company1 = item.Element("Company").Value,
AirLineCode1 = item.Element("AirlineCode").Value,
StartDrome1 = item.Element("StartDrome").Value,
EndDrome1 = item.Element("ArriveDrome").Value,
StartTime1 = item.Element("StartTime").Value,
EndTime1 = item.Element("ArriveTime").Value,
AirLineStop1 = item.Element("AirlineStop").Value,
Week1 = item.Element("Week").Value
}).ToList();
foreach (AirLine1 al in airlinelist)
{
TextBlock t = new TextBlock();
t.Text = "航空公司:" + al.Company1;
t.FontSize = 24;
TextBlock t1 = new TextBlock();
t1.Text = "航班号:" + al.AirLineCode1;
t1.FontSize = 24;
TextBlock t2 = new TextBlock();
t2.Text = "出发机场:" + al.StartDrome1;
t2.FontSize = 24;
TextBlock t3 = new TextBlock();
t3.Text = "到达机场:" + al.EndDrome1;
t3.FontSize = 24;
TextBlock t4 = new TextBlock();
t4.Text = "出发时间:" + al.StartTime1;
t4.FontSize = 24;
TextBlock t5 = new TextBlock();
t5.Text = "到达时间:" + al.EndTime1;
t5.FontSize = 24;
TextBlock t6 = new TextBlock();
t6.Text = "经停:" + al.AirLineStop1 + "次";
t6.FontSize = 24;
TextBlock t7 = new TextBlock();
t7.Text = "飞行周期:" + al.Week1;
t7.FontSize = 24;
StackPanel sp = new StackPanel();
sp.Margin = new Thickness(0, 10, 0, 0);
sp.Width = 400;
sp.Children.Add(t);
sp.Children.Add(t1);
sp.Children.Add(t2);
sp.Children.Add(t3);
sp.Children.Add(t4);
sp.Children.Add(t5);
sp.Children.Add(t6);
sp.Children.Add(t7);
stackPanel1.Children.Add(sp);
}
}
catch (Exception ee)
{
MessageBox.Show("很抱歉,找不到您所要查询的信息,请稍后再试");
}
}
这个例子是一个航班查询的实力,在使用之前需要先添加WebService 服务,这里主要是讲XML存储本地数据和解析,因此对于web应用方便就不多讲了,如果有需要源码的朋友可以给我留个邮箱,到时发到你们邮箱。
运行的效果图:
三、数据的序列化和反序列化
1、序列化:
事先写好一个类例如:
People类
public class People
{
string Name{get;set;}
string Address{get;set;}
}
static void Main(string[] args)
{
var people = new People()
{
Name = “Olive",
Address="北京"
};
序列化:
//序列化将序列化People类的实例写入内存流中,需要引入using System.Runtime.Serialization;命名空间
var serializer = new DataContractJsonSerializer(typeof(People));
var stream = new MemoryStream();
serializer.WriteObject(stream, people);
//从内存流中中读到string中,即生成Json
byte[] dataBytes = new byte[stream.Length];
stream.Position = 0;
stream.Read(dataBytes, 0,(int) stream.Length);
string dataString = Encoding.UTF8.GetString(dataBytes);
Console.WriteLine(dataString);//即:序列化之后的Json格式的字符串
Console.ReadLine();
反序列化:
//反序列化先将json格式的字符串读入到内存中,然后在经过反序列化成类的对象
var mStream = new MemoryStream(Encoding.UTF8.GetBytes(dataString));
DataContractJsonSerializer dcJoson = new DataContractJsonSerializer(typeof(People));
People pp=(People) dcJoson.ReadObject(mStream);
Console.WriteLine(pp.Name);//输入People类对象的Name属性
Console.ReadLine();
}
序列化和反序列化主要用到了using System.Runtime.Serialization;命名空间下的 DataContractJsonSerializer类的两个实例方法
WirteObject()和ReadObject()
在WindowsPhone中解析Json需要同时加入using System.Runtime.Serialization;命名空间和System.Servicemodel.Web命名空间
其解析方法和桌面程序解析差不多,
主要是在异步回调函数中将返回的字符串进行解析,先创建和返回数据相映射的类,然后在用DataContractJsonSerilzer类的实例方法
ReadObject()将返回数据反序列化为类的实例。
先来讲一下XML解析本地的数据,
一、首先应在当前项目下创建一个.xml文件,例如本例的SpokenEnglish.xml文件
文件的格式如下:
<?xml version="1.0" encoding="utf-8" ?>
<SpokenEnglish>
<Items>
<Item>
<Spoken>1. I see.</Spoken>
<Chinese>我明白了。</Chinese>
</Item>
<Item>
<Spoken>2. I quit!</Spoken>
<Chinese>我不干了! </Chinese>
</Item>
<Item>
<Spoken>3. Let go! </Spoken>
<Chinese>放手! </Chinese>
</Item>
<Item>
<Spoken>4. Me too.</Spoken>
<Chinese>我也是。</Chinese>
</Item>
<Item>
<Spoken>5. My god!</Spoken>
<Chinese>天哪!</Chinese>
</Item>
</Items>
</SpokenEnglish>
这是本地的数据存储文件
下边再建一个与源数据相映射的类:SpokenEnglishes.cs
如下:
在使用之前应先加入以下命名空间
using System.Collections.Generic;//对应List<T>泛型集合
using System.Linq;//对应Element.load().Select();
using System.Xml.Linq;//对应XDocument
namespace XMLAndJSON
{
public class SpokenEnglishes
{
public static readonly string xmlPath;//数据文件的路径
public string Spoken
{
get;
set;
}
public string Chinese
{
get;
set;
}
static SpokenEnglishes()
{
xmlPath = @"SpokenEnglish.xml";
}
XML解析方法一
public static List<SpokenEnglishes> GetAllSpokenEnglishes()//获取数据文件中的数据,并映射为SpokenEnglishes类
{
return XElement.Load(xmlPath).Element("Items").Descendants("Item").Select(item => new SpokenEnglishes()
{
Spoken = item.Element("Spoken").Value,
Chinese=item.Element("Chinese").Value
}).ToList();
}
XML解析方法二
public static List<SpokenEnglishes> GetAll()//与上一个函数功能相同,只不过是另一个XML解析方法
{
XDocument xDocument = XDocument.Load("SpokenEnglish.xml");
return xDocument.Descendants("Item").Select(item => new SpokenEnglishes()
{
Chinese = item.Element("Chinese").Value,
Spoken = item.Element("Spoken").Value
}).ToList();
}
}
}
然后就是在MainPage.xaml页进行UI的布局和数据的绑定
主要代码如下:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Height="591" HorizontalAlignment="Left" Margin="10,10,0,0" Name="listBox1" VerticalAlignment="Top"
Width="440" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Name="spoken" Text="{Binding Spoken}"/>//与SpokenEnglish类属性相绑定
<TextBlock Text="{Binding Chinese}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
然后在MainPage.xaml.cs页的构造函数里为listbox1指定数据源
代码如下: listBox1.ItemsSource = SpokenEnglishes.GetAll();//静态类SpokenEnglishes调用其GetAll()方法获取List<SpokenEnglishes>的集合
运行效果如下:
二、用XML解析Web服务返回的数据跟解析本地数据的方法差不多,
大体步骤如下:
1、先根据返回的XML数据格式创建与数据相互映射的实体类:
例如:AirLine类,航班信息类
public class AirLine1
{
public string Company1 { get; set; }
public string AirLineCode1 { get; set; }
public string StartDrome1 { get; set; }
public string EndDrome1 { get; set; }
public string StartTime1 { get; set; }
public string EndTime1 { get; set; }
public string AirLineStop1 { get; set; }
public string Week1 { get; set; }
}
然后在异步返回函数中做出如下的解析:
void airws_getDomesticAirlinesTimeCompleted(object sender, AirLineWS.getDomesticAirlinesTimeCompletedEventArgs e)
{
try
{//关键的解析部分
List<AirLine1> airlinelist = e.Result.Nodes.Descendants("AirlinesTime").Select(
item => new AirLine1()
{
Company1 = item.Element("Company").Value,
AirLineCode1 = item.Element("AirlineCode").Value,
StartDrome1 = item.Element("StartDrome").Value,
EndDrome1 = item.Element("ArriveDrome").Value,
StartTime1 = item.Element("StartTime").Value,
EndTime1 = item.Element("ArriveTime").Value,
AirLineStop1 = item.Element("AirlineStop").Value,
Week1 = item.Element("Week").Value
}).ToList();
foreach (AirLine1 al in airlinelist)
{
TextBlock t = new TextBlock();
t.Text = "航空公司:" + al.Company1;
t.FontSize = 24;
TextBlock t1 = new TextBlock();
t1.Text = "航班号:" + al.AirLineCode1;
t1.FontSize = 24;
TextBlock t2 = new TextBlock();
t2.Text = "出发机场:" + al.StartDrome1;
t2.FontSize = 24;
TextBlock t3 = new TextBlock();
t3.Text = "到达机场:" + al.EndDrome1;
t3.FontSize = 24;
TextBlock t4 = new TextBlock();
t4.Text = "出发时间:" + al.StartTime1;
t4.FontSize = 24;
TextBlock t5 = new TextBlock();
t5.Text = "到达时间:" + al.EndTime1;
t5.FontSize = 24;
TextBlock t6 = new TextBlock();
t6.Text = "经停:" + al.AirLineStop1 + "次";
t6.FontSize = 24;
TextBlock t7 = new TextBlock();
t7.Text = "飞行周期:" + al.Week1;
t7.FontSize = 24;
StackPanel sp = new StackPanel();
sp.Margin = new Thickness(0, 10, 0, 0);
sp.Width = 400;
sp.Children.Add(t);
sp.Children.Add(t1);
sp.Children.Add(t2);
sp.Children.Add(t3);
sp.Children.Add(t4);
sp.Children.Add(t5);
sp.Children.Add(t6);
sp.Children.Add(t7);
stackPanel1.Children.Add(sp);
}
}
catch (Exception ee)
{
MessageBox.Show("很抱歉,找不到您所要查询的信息,请稍后再试");
}
}
这个例子是一个航班查询的实力,在使用之前需要先添加WebService 服务,这里主要是讲XML存储本地数据和解析,因此对于web应用方便就不多讲了,如果有需要源码的朋友可以给我留个邮箱,到时发到你们邮箱。
运行的效果图:
三、数据的序列化和反序列化
1、序列化:
事先写好一个类例如:
People类
public class People
{
string Name{get;set;}
string Address{get;set;}
}
static void Main(string[] args)
{
var people = new People()
{
Name = “Olive",
Address="北京"
};
序列化:
//序列化将序列化People类的实例写入内存流中,需要引入using System.Runtime.Serialization;命名空间
var serializer = new DataContractJsonSerializer(typeof(People));
var stream = new MemoryStream();
serializer.WriteObject(stream, people);
//从内存流中中读到string中,即生成Json
byte[] dataBytes = new byte[stream.Length];
stream.Position = 0;
stream.Read(dataBytes, 0,(int) stream.Length);
string dataString = Encoding.UTF8.GetString(dataBytes);
Console.WriteLine(dataString);//即:序列化之后的Json格式的字符串
Console.ReadLine();
反序列化:
//反序列化先将json格式的字符串读入到内存中,然后在经过反序列化成类的对象
var mStream = new MemoryStream(Encoding.UTF8.GetBytes(dataString));
DataContractJsonSerializer dcJoson = new DataContractJsonSerializer(typeof(People));
People pp=(People) dcJoson.ReadObject(mStream);
Console.WriteLine(pp.Name);//输入People类对象的Name属性
Console.ReadLine();
}
序列化和反序列化主要用到了using System.Runtime.Serialization;命名空间下的 DataContractJsonSerializer类的两个实例方法
WirteObject()和ReadObject()
在WindowsPhone中解析Json需要同时加入using System.Runtime.Serialization;命名空间和System.Servicemodel.Web命名空间
其解析方法和桌面程序解析差不多,
主要是在异步回调函数中将返回的字符串进行解析,先创建和返回数据相映射的类,然后在用DataContractJsonSerilzer类的实例方法
ReadObject()将返回数据反序列化为类的实例。
相关文章推荐
- Android解析xml和json格式数据
- Android中的xml、json格式数据解析
- Android访问网络:服务端返回XML或JSON格式数据,Android 进行解析并使用ListView显示
- S2SH+ajax+json-----jQuery中$.ajax解析xml、json格式数据
- Android访问网络系列之--服务端返回XML或JSON格式数据,Android 进行解析并显示
- kettle(PDI)解析xml、json等格式数据的方法
- Android中的xml、json格式数据解析
- Json解析 开发实践 XML和JSON格式的数据
- Android访问网络系列之--服务端返回XML或JSON格式数据,Android 进行解析并显示
- javascript 解析ajax返回的xml和json格式的数据
- 简介Objective-C解析XML与JSON数据格式的方法
- javascript解析ajax返回的xml和json格式数据实例详解
- jQuery中$.ajax解析xml、json格式数据
- Swift学习笔记(2)网络数据交换格式(XML,JSON)解析 [iOS实战 入门与提高卷]
- javascript 解析ajax返回的xml和json格式的数据
- Android Json 数据解析 II 之 ------xml ,json 两种数据格式之对比
- 常用数据格式xml和json解析
- xml 和json 数据格式及解析
- 读书笔记2,xml和json数据格式的解析
- ajax传输的数据格式(XML,json)怎么获取解析