外部WebService返回的是xml太重了. 而JSON是web的新标准.所以要包装一下.

使用NewtonSoft.JSON的dll里面的JsonConvert.SerializeXmlNode方法

        private string xml2json(string xml, string TagName, string CDataName)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            XmlNode node = xmlDoc.GetElementsByTagName(TagName).Item(0);
            var json = JsonConvert.SerializeXmlNode(node, Newtonsoft.Json.Formatting.None, true);

            string jsonText = Regex.Replace(json, "(?<=\")(@)(?!.*\":\\s )", string.Empty, RegexOptions.IgnoreCase);
            jsonText = jsonText.Replace("#cdata-section", CDataName);
            return jsonText;
        }

  

posted on 2015-02-03 00:39  Gu  阅读(290)  评论(0编辑  收藏  举报