【问题标题】:Pass date to web service as object property将日期作为对象属性传递给 Web 服务
【发布时间】:2012-12-19 19:42:56
【问题描述】:

我是网络服务的新手,我试图找到解决方案,但找不到任何解决方案。

我正在尝试将对象作为参数发送到 Web 服务。

    [WebMethod]
    public bool CreatePatientService(Patient p)
    {
        AdminService AsRef = new AdminService();
        return AsRef.CreatePatient(p);
    }

我的病人分类如下:

[Serializable]
public class Patient
{

    public string NIC { get; set; }
    public string FullName { get; set; }
    public string FirstName { get; set; }
    public string Surname { get; set; }
    public string Title { get; set; }
    public string Gender { get; set; }
    public string CivilStatus { get; set; }
    public DateTime DOB { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
}

然后我使用 SOAPUi 调用 Web 服务。

我将请求调用如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:CreatePatientService>
         <!--Optional:-->
         <tem:p>
            <!--Optional:-->
            <tem:NIC>15487236</tem:NIC>
            <!--Optional:-->
            <tem:FullName>awdss</tem:FullName>
            <!--Optional:-->
            <tem:FirstName>qewretr</tem:FirstName>
            <!--Optional:-->
            <tem:Surname>qscv</tem:Surname>
            <!--Optional:-->
            <tem:Title>Mr</tem:Title>
            <!--Optional:-->
            <tem:Gender>M</tem:Gender>
            <tem:CivilStatus>S</tem:CivilStatus>
            <tem:DOB>01/02/2002</tem:DOB>
            <!--Optional:-->
            <tem:Address1>nikhno</tem:Address1>
            <!--Optional:-->
            <tem:Address2>asdf</tem:Address2>
            <!--Optional:-->
            <tem:Address3>125</tem:Address3>
         </tem:p>
      </tem:CreatePatientService>
   </soapenv:Body>
</soapenv:Envelope>

然后它给出以下错误。我该如何解决?

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Client</faultcode>
         <faultstring>System.Web.Services.Protocols.SoapException: Server was unable to read request. ---> System.InvalidOperationException: There is an error in XML document (19, 49). ---> System.FormatException: Input string was not in a correct format.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer&amp; number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseUInt32(String value, NumberStyles options, NumberFormatInfo numfmt)
   at System.UInt16.Parse(String s, NumberStyles style, NumberFormatInfo info)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read2_TblPatient(Boolean isNullable, Boolean checkType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read7_CreatePatientService()
   at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer8.Deserialize(XmlSerializationReader reader)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
   --- End of inner exception stack trace ---
   at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()</faultstring>
         <detail/>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

如果我删除了 DOB(日期),那么它可以正常工作。

我参考以下帖子。但不能正常工作。

Proper DateTime Format for a Web Service

请帮忙..谢谢...

【问题讨论】:

    标签: .net web-services c#-4.0 wsdl


    【解决方案1】:

    DateTime 需要有时间值和日期,所以你错过了小时、分钟、秒等的事实是破坏它的原因。

    yyyy-MM-ddTHH:mm:ss.fffffffzzzzzz is the expected format.
    
    e.g. 2002-02-01T00:00:00.0
    

    XML deserialize DateTime Format

    【讨论】:

      猜你喜欢
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 2014-01-08
      • 2017-06-21
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多