关于Xml的序列化和反序列化:
可以看这篇文章,http://www.cnblogs.com/Windows-phone/p/3243575.html
WebService解析返回的数据DataSet
<?xml version="1.0" encoding="utf-8" ?> - <DataSet xmlns="http://WebXml.com.cn/"> - <xs:schema id="getRegion" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> - <xs:element name="getRegion" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> - <xs:complexType> - <xs:choice minOccurs="0" maxOccurs="unbounded"> - <xs:element name="Province"> - <xs:complexType> - <xs:sequence> <xs:element name="RegionID" type="xs:string" minOccurs="0" /> <xs:element name="RegionName" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> - <xs:element name="Country"> - <xs:complexType> - <xs:sequence> <xs:element name="RegionID" type="xs:string" minOccurs="0" /> <xs:element name="RegionName" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> - <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"> - <getRegion xmlns=""> - <Province diffgr:id="Province1" msdata:rowOrder="0"> <RegionID>3113</RegionID> <RegionName>黑龙江</RegionName> </Province> - <Province diffgr:id="Province2" msdata:rowOrder="1"> <RegionID>3114</RegionID> <RegionName>吉林</RegionName> </Province> - <Province diffgr:id="Province3" msdata:rowOrder="2"> <RegionID>3115</RegionID> <RegionName>辽宁</RegionName> </Province> <Province diffgr:id="Province4" msdata:rowOrder="3"> <RegionID>3116</RegionID> <RegionName>内蒙古</RegionName> </Province> - <Province diffgr:id="Province5" msdata:rowOrder="4"> <RegionID>3117</RegionID> <RegionName>河北</RegionName> </Province> - <Province diffgr:id="Province6" msdata:rowOrder="5"> <RegionID>3118</RegionID> <RegionName>河南</RegionName> </Province> - <Province diffgr:id="Province7" msdata:rowOrder="6"> <RegionID>3119</RegionID> <RegionName>山东</RegionName> </Province> - ....... </getRegion>...
在我们获取了这个Xml数据之后,我们如何解析这段数据,并将该数据转换成Province集合呢?
代码如下:
static string head = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Province>"; //static string tail = "</{0}>"; static string tail = "</Province>";
/// <summary> /// 获取多个实体对象 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="aoxe"></param> /// <returns></returns> public static ObservableCollection<T> GetAllEntitiesForDataSetXml<T>(ArrayOfXElement aoxe) { ObservableCollection<T> listT = new ObservableCollection<T>(); XmlSerializer serializer = new XmlSerializer(typeof(T)); T t = default(T); try { ].LastNode));//取出第二个结点中的内容 StringBuilder sb = new StringBuilder(); foreach (var item in content.Nodes()) { //sb.Append(string.Format(head, typeof(T).Name)); sb.Append(head); foreach (var obj in ((XElement)item).Nodes()) { sb.Append(obj.ToString()); } //sb.Append(string.Format(tail, typeof(T).Name)); sb.Append(tail); string ss = sb.ToString(); StringReader sr = new StringReader(ss); //反序列化,并将反序列化结果值赋给变量i t = (T)serializer.Deserialize(sr); listT.Add(t); sr.Close(); sr.Dispose(); sb.Clear(); } } catch { } return listT; }