Recently, Brother Shuang wanted to try the developer mode of WeChat public account, and he applied for an account on the WeChat public platform. Then I went to see the interface guide of the open platform, he put a link in the group, WeChat has been overheated recently, I also clicked in curiously.
Click on the message interface guide, we can see that the message interface of the public platform uses XML for data interaction. For the convenience of data manipulation, we usually serialize and deserialize XML. Serialization is the process of converting an object's state into a format that can be maintained or transferred. The opposite of serialization is deserialization, which converts streams into objects. These two processes combine to make it easy to store and transfer data.
Let's talk about serialization first, let's take the XML format of replying to graphic messages as an example, because this format basically includes all the above message push and message reply.
When <xml><Articles>we receive the data response from the public platform POST to return the output, we use XML serialization, that is, to convert the response data object into the XML data format provided by the public platform, which is convenient for data transmission. Serialization Here I wrote a simple example code in C#. In order to obtain the serialized string, There is no direct output, of course in your project you can directly Response.Output.
It is mentioned here that the name of the XML root element generated by the Serialize method of the XmlSerializer class defaults to the name of the serialization class. Why do I feel so tongue-twisting when I read this sentence? And I don't want to use a meaningless word like xml as a class name, we need to add a property tag to the serialization class [XmlRoot(ElementName = "xml")] to specify that the generated XML element is named xml. If you don't add this tag, if your serialization class name does not correspond to the root element name of XML, you will always get a document format error.
After getting the root element, we need to solve the problem of the collection node of Articles, at this time we need to create a new list of strong types, and the variable is called Articles public property.
XmlArrayItemAttribute is a derived type that specifies that the XmlSerializer can be placed in a serialized array. After creating the class, debugging and looking at it, there is basically no big problem.
Let's talk about deserialization. When an ordinary WeChat user sends a message to the public account, the WeChat server will POST the message to the filled URL. At this time, we need to deserialize the received data into the objects we need for easy operation and storage. Usually we use Request.Form["param"] to get the data of the Post, and many developers find that the data of POST has no parameters when looking at the interface documentation, and they are confused and don't know how to get the data. Here we use Request.InputStream to obtain the content of the incoming HTTP entity body. For more information, please refer to this article: http://blog.sina.com.cn/s/blog_9fa44b550101ee3r.html
Deserialization Here I wrote a simple example code in C#.
Here, because it is a string that directly copies the xml sample data that replies to the graphic message, the overload of Deserialize (TextReader textReader) is used, of course, you can directly use the overload of Deserialize (Stream stream) in the project.
For questions about XML serialization, see this article on MSDN about XML and SOAP serialization
I have put the relevant instance code file on github at XmlSerializerExample
Write in a hurry and simply make a mark. I didn't eat enough at night and now I feel nauseous. I have to go to bed quickly. If there are any mistakes, please correct them.
Original link:
http://imr3.com/tech/weixin-message-api-serialize
|