WebService XML SoapFormatter

KingCruel發表於2021-01-05

管理 NuGet 程式包
SoapFormatter

使用 FileStream 建立檔案【SoapSerialize.xml】

[HttpGet()]
public IEnumerable<WeatherForecast> Get()
{
  #region SoapFormatter Serialize
  Company companySerialize = new Company { name = "1234", code = 1 };
  SoapFormatter soapCreate = new SoapFormatter();
  using (FileStream streamCreate = new FileStream(@"D:\SoapSerialize.xml", FileMode.Create))
  {
    soapCreate.Serialize(streamCreate, companySerialize);
  }
  #endregion
}

使用 FileStream 讀取檔案【SoapSerialize.xml】

[HttpGet()]
public IEnumerable<WeatherForecast> Get()
{
  #region SoapFormatter Deserialize
  Company companyDeserialize = null;
  SoapFormatter soapOpen = new SoapFormatter();
  using (FileStream streamOpen = new FileStream(@"D:\SoapSerialize.xml", FileMode.Open))
  {
    companyDeserialize = (Company)soapOpen.Deserialize(streamOpen);
  }
  var name = companyDeserialize.name;
  #endregion
}

Company

[Serializable]
public class Company
{
  public int code { get; set; }

  public string name { get; set; }
}

SoapSerialize.xml 檔案

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/clr/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <a1:Company id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/NewCapital.WebAPI.Controllers/NewCapital.WebAPI%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
      <_x003C_code_x003E_k__BackingField>1</_x003C_code_x003E_k__BackingField>
      <_x003C_name_x003E_k__BackingField id="ref-2">1234</_x003C_name_x003E_k__BackingField>
    </a1:Company>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

*
*
*

相關文章