开发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;
        }

 

自己学习记录,仅供参考。

posted @ 2012-08-01 10:42  十一_x  阅读(337)  评论(0编辑  收藏  举报