zoukankan      html  css  js  c++  java
  • WebService 返回json格式和返回xml格式的数据

    返回json格式

    //using System.Web.Script.Services;
            [WebMethod]
            [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
            public void HelloWorld()
            {
                Context.Response.Clear();
                Context.Response.ContentType = "application/json";
                Model.User.User_User user = new Model.User.User_User();
                user.UserName = "我们";
                user.UID = 1;
                user.UserPassWord = "123456";
                Jayrock.Json.JsonTextWriter writer = new Jayrock.Json.JsonTextWriter();
                Jayrock.Json.Conversion.JsonConvert.Export(user, writer);
                Context.Response.Write(writer.ToString());
            }

    效果:

    返回xml格式

    [WebMethod]
            public XmlDocument HelloWorld()
            {
                downList doo = new downList();
                List<file> flist = new List<file>();
                file f = new file();
                f.name = "test";
                f.value = "t";
                flist.Add(f);
                List<sql> slist = new List<sql>();
                List<desc> dlist = new List<desc>();
                version ver = new version();
    
                doo.version = ver;
                doo.sqlList = slist;
                doo.fileList = flist;
    
                XmlDocument XmlDoc = new XmlDocument();
                string xmlstring = Utility.Tool.Serialize(doo);
                XmlDoc.LoadXml(xmlstring);
                return XmlDoc;
            }
    public class downList
        {
            public List<file> fileList;
    
            public List<sql> sqlList;
    
            public version version;
    
            public List<desc> descList; 
    
            public int result;
        }
    
    
    
        public class sql
        {
            [XmlText]
            public string value;
        }
    
        [Serializable]
        public class file
        {
            [XmlAttribute]
            public string name;
    
            [XmlText]
            public string value;
        }
    
        public class desc
        {
            [XmlText]
            public string value;
        }
    
        [Serializable]
        public class version
        {
            [XmlAttribute]
            public string name;
        }
    /// <summary>
            /// 将指定的对象序列化为XML格式的字符串并返回。
            /// </summary>
            /// <param name="o">待序列化的对象</param>
            /// <returns>返回序列化后的字符串</returns>
            public static string Serialize(Object o)
            {
                string xml = "";
                try
                {
                    XmlSerializer serializer = new XmlSerializer(o.GetType());
                    using (MemoryStream mem = new MemoryStream())
                    {
                        using (XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8))
                        {
                            writer.Formatting = Formatting.Indented;
                            XmlSerializerNamespaces n = new XmlSerializerNamespaces();
                            n.Add("", "");
                            serializer.Serialize(writer, o, n);
    
                            mem.Seek(0, SeekOrigin.Begin);
                            using (StreamReader reader = new StreamReader(mem))
                            {
                                xml = reader.ReadToEnd();
                            }
                        }
                    }
                }
                catch { xml = ""; }
                return xml;
            }

    效果:

  • 相关阅读:
    【转】网络字节序与主机字节序
    VC之美化界面篇 (转)
    VS2008编译的程序在某些机器上运行提示“由于应用程序配置不正确,应用程序未能启动”的问题(转)
    符验手记
    一友人昨夜接到电话,发生何事
    [转]众VC论道IT峰会:投资是否靠运气
    路过一个小摊,看到一个有趣的现象
    PJSUA提示要注册线程的解决办法
    彩票股票金融与运气之研究(五) 明敌
    随手测一局婚姻,留验
  • 原文地址:https://www.cnblogs.com/hougelou/p/3793156.html
Copyright © 2011-2022 走看看