Summary of 3 cases of. net web API receiving XML format data

Time:2019-10-5

Preface

ASP.NET Web API is a framework that can easily build a wide range of HTTP service clients, including browsers and mobile devices. NET framework is an ideal platform for building RESTful applications.

This article mainly introduces to you about. net web API receiving XML format data related content, the following words do not say much, let’s take a look at the detailed introduction

Three methods for web API to receive XML

Some time ago, I received a task to write a small interface to receive the short message XML data returned from the Java side.

When I first got the project, my first thought was to send XML format data to me in the form of the other side, and imagine three situations.

I started with a string of XML formatted strings. as follows

<?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 [Reply Content]> </content> 
  <userId>id</userId> 
  <extNum>Extension </extNum> 
  <recvNum/> 
 </pushSmsForm> 
 <pushSmsForm> 
  <eprId>0</eprId> 
  <mobile>13560739261</mobile> 
  <msgId>30217001</msgId> 
  <content> <![CDATA [Reply Content]> </content> 
  <userId>id</userId> 
  <extNum>Extension </extNum> 
  <recvNum/> 
 </pushSmsForm> 
 </list> 
</returnForm>

thinking

This basic knowledge of XML I will not introduce one by one, simply say <! [CDATA []]> logo, the content is unlimited (except < and >). Continuing with the idea, the document only mentions the post method, so I think of three possibilities:

  1. The other party submits an XML file through the form form. I need to read the file with the file stream and convert it into an entity class.
  2. The other party posts a string, and I receive the string and convert it into an entity class.
  3. The other party adds the XML string (text/xml) directly to the request.

First, build two helper classes for XML deserialization

/// <summary> 
  /// deserialization 
  /// </summary> 
  /// <param name="type">type</param> 
  /// <param name="xml">XML string </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> 
  /// deserialization 
  /// </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);
  }

Generate classes by using XML functionality of visual studio (edit – > selective paste – > 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;
   }
  }
 }

After generating classes, note that the data types need to be modified as required.

Next, I’ll start writing the first interface, the simplest way to receive strings directly.

[HttpPost, Route("get/context")]
  public int getContext(context_ context)
  {
   bool result = false;
   // Short message data
   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;
  }

Here I define a class context_, which calls the previously written method to deserialize the entity class, and then writes it to the database.

The second interface receives the XML in the request (text/xml)

[HttpPost, Route("get/context")]
  public int getContext(context_ context)
  {
   bool result = false;
   // Short message data
   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;
  }

Here you read the contents of the request, HttpContext. Current. Request. InputStream

The third is to read files.


  [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;
  }

Traverse the file content to get data

summary

Above is the whole content of this article. I hope that the content of this article has a certain reference value for everyone’s study or work. If you have any questions, you can leave a message and exchange it. Thank you for your support to developpaer.