开发Tips—Deserialize Xml
View Code
/// <summary> /// 归属地查询响应 /// </summary> [Serializable] [XmlRoot(Namespace = "http://api.showji.com/Locating/", ElementName = "QueryResponse")] public class QueryResponse { /// <summary> /// 手机号 /// </summary> [XmlElement("Mobile")] public string Mobile { get; set; } /// <summary> /// 查询结果,bool值 /// </summary> [XmlElement("QueryResult")] public string QueryResult { get; set; } /// <summary> /// 归属地省份 /// </summary> [XmlElement("Province")] public string Province { get; set; } /// <summary> /// 归属地城市 /// </summary> [XmlElement("City")] public string City { get; set; } /// <summary> /// 归属地区号 /// </summary> [XmlElement("AreaCode")] public string AreaCode { get; set; } /// <summary> /// 归属地邮编 /// </summary> [XmlElement("PostCode")] public string PostCode { get; set; } /// <summary> /// 运营商 /// </summary> [XmlElement("Corp")] public string Corp { get; set; } /// <summary> /// 卡制式 /// </summary> [XmlElement("Card")] public string Card { get; set; } }
View Code
/// <summary> /// 手机号归属地信息 /// </summary> /// <param name="num"></param> /// <returns></returns> private QueryResponse MobileInfo(string num) { string uri = string.Format(string.Format("http://api.showji.com/Locating/zhoumoyukuai.aspx?m={0}&output=xml&callback=querycallback", num)); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(uri); XmlNode node = xmlDoc.DocumentElement.ParentNode; //Xml序列化 XmlSerializer xmlSerializer = new XmlSerializer(typeof(QueryResponse)); QueryResponse query = (QueryResponse)xmlSerializer.Deserialize(new XmlNodeReader(node)); return query; }
自己学习记录,仅供参考。
作者:十一_x
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议