.net webapi 接收 xml 格式数据的三种情况

webapi 接收 xml 的三种方法

前段时间接到一个任务写一个小接口,要接收java端返回过来的短信xml数据。

刚拿到项目,我的第一想法是对方会以什么形式发送xml格式的数据给我呢,设想三种情况。

我一开始拿到手上的是一串xml格式字符串。如下

<?xml version="1.0" encoding="utf-8"?>
<returnForm>
<type>2</type>
<count>1</count>
<list>
<pushSmsForm>
<eprId>0</eprId>
<mobile>13560739262</mobile>
<msgId>30217002</msgId>
<content> <![CDATA[回复内容]]> </content>
<userId>id</userId>
<extNum>扩展号</extNum>
<recvNum/>
</pushSmsForm>
<pushSmsForm>
<eprId>0</eprId>
<mobile>13560739261</mobile>
<msgId>30217001</msgId>
<content> <![CDATA[回复内容]]> </content>
<userId>id</userId>
<extNum>扩展号</extNum>
<recvNum/>
</pushSmsForm>
</list>
</returnForm>

思路

这个xml的基本知识我就不一一介绍了,简单说一下<![CDATA[ ]]>标志,里面的内容是没有限制的(除了< 和 >)。继续讲思路,文档中只说了是post方法,所以我想到了三种可能:

  • 对方通过form表单提交了一个xml文件,我需要用文件流(filestream)读取文件,并进行转化为实体类
  • 对方通过post了一个字符串过来,我接收字符串并转化为实体类。
  • 对方直接在请求里添加了xml字符串(text/xml)
  • 先构建两个xml反序列化的帮助类
        /// <summary> 
        /// 反序列化 
        /// </summary> 
        /// <param name="type">类型</param> 
        /// <param name="xml">XML字符串</param>
        /// <returns></returns> 
        public static object Deserialize(Type type, string xml)
        {
            using (StringReader sr = new StringReader(xml))
            {
                XmlSerializer xmldes = new XmlSerializer(type);
                return xmldes.Deserialize(sr);
            }
        }         /// <summary> 
        /// 反序列化 
        /// </summary> 
        /// <param name="type"></param> 
        /// <param name="xml"></param> 
        /// <returns></returns>          public static object Deserialize(Type type, Stream stream)
        {
            XmlSerializer xmldes = new XmlSerializer(type);
            return xmldes.Deserialize(stream);
        }
  • 利用visual studio自带的功能xml生成类(编辑-->选择性粘贴-->paste xml as classes)
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class returnFormPushSmsForm
    {
        private int eprIdField;
        private string mobileField;
        private string msgIdField;
        private string contentField;
        private string userIdField;
        private string extNumField;
        private string recvNumField;         /// <remarks/>
        public int eprId
        {
            get
            {
                return this.eprIdField;
            }
            set
            {
                this.eprIdField = value;
            }
        }         /// <remarks/>
        public string mobile
        {
            get
            {
                return this.mobileField;
            }
            set
            {
                this.mobileField = value;
            }
        }         /// <remarks/>
        public string msgId
        {
            get
            {
                return this.msgIdField;
            }
            set
            {
                this.msgIdField = value;
            }
        }         /// <remarks/>
        public string content
        {
            get
            {
                return this.contentField;
            }
            set
            {
                this.contentField = value;
            }
        }         /// <remarks/>
        public string userId
        {
            get
            {
                return this.userIdField;
            }
            set
            {
                this.userIdField = value;
            }
        }         /// <remarks/>
        public string extNum
        {
            get
            {
                return this.extNumField;
            }
            set
            {
                this.extNumField = value;
            }
        }         /// <remarks/>
        public string recvNum
        {
            get
            {
                return this.recvNumField;
            }
            set
            {
                this.recvNumField = value;
            }
        }
    }

生成类之后注意一下数据类型需要根据需求做一些修改

  • 接下来就开始写第一个接口,最简单的直接接收字符串
        [HttpPost, Route("get/context")]
        public int getContext(context_ context)
        {
            bool result = false;
            //短信数据
            returnForm context_data =
XmlSerializeUtil.Deserialize(typeof(returnForm), context.context) as returnForm;
            if (context_data != null && context_data.count > 0)
            {
                result = UpdateDB(context_data);
            }
            return result ? 100 : 500;
        }

这里我定义了一个类context_,调用之前写的方法反序列化实体类,再写入数据库

  • 第二个接口时接收请求中的xml(text/xml)
        [HttpPost, Route("get/context")]
        public int getContext(context_ context)
        {
            bool result = false;
            //短信数据
            returnForm context_data = XmlSerializeUtil.Deserialize(typeof(returnForm),
HttpContext.Current.Request.InputStream) as returnForm;
            if (context_data != null && context_data.count > 0)
            {
                result = UpdateDB(context_data);
            }
            return result ? 100 : 500;
        }

这里读取了请求中的内容,HttpContext.Current.Request.InputStream

  • 第三种是读取文件
        [HttpPost, Route("get/context")]
        public int getContext(context_ context)
        {
            bool result = false;
            HttpFileCollection files = HttpContext.Current.Request.Files;
            foreach (string key in files.AllKeys)
            {
                HttpPostedFile file1 = files[key];
                file1.InputStream.ToString();
                returnForm context_data =
XmlSerializeUtil.Deserialize(typeof(returnForm), file1.InputStream) as returnForm;
                if (context_data != null && context_data.count > 0)
                {
                    result = UpdateDB(context_data);
                }
            }
            return result ? 100 : 500;
        }

遍历文件内容,获取数据

上一篇:js中apply方法的使用


下一篇:servlet 文件上传简易版