【问题标题】:Proper XML serialization for web service PUT/POST methodWeb 服务 PUT/POST 方法的正确 XML 序列化
【发布时间】:2015-12-17 12:59:48
【问题描述】:

我有 WCF 网络服务,它有下一个 PUT 方法

[OperationContract]
[WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml)]
void ScanPatient(PatientInfo patientInfo);

//...

[XmlRoot("PatientInfo")]
public class PatientInfo
{
    [XmlElement("FirstName")]
    public string FirstName { get; set; }
    [XmlElement("LastName")]
    public string LastName { get; set; }
    [XmlElement("SSN")]
    public string SSN { get; set; }
    [XmlElement("Birthday")]
    public DateTime? Birthday { get; set; }
    [XmlElement("RequestedClientID")]
    public Guid RequestedClientID { get; set; }
    [XmlElement("patientId")]
    public Guid patientId { get; set; }
}

我正在使用 WebRequest 与此服务进行通信

private void ExecuteWebServiceCommand(string method, string command, string parameters = "")
{
    var request = (HttpWebRequest)WebRequest.Create(new Uri(command));
    request.ContentType = "application/xml";
    request.Method = method;

    string responseFromServer = null;
    byte[] bytes = Encoding.UTF8.GetBytes(parameters);

    request.ContentLength = bytes.Length;
    using (var newStream = request.GetRequestStream())
    {
        newStream.Write(bytes, 0, bytes.Length);
    }
    var response = request.GetResponse();
}

如果使用 parameters 的下一个格式,它可以完美运行:

<PatientInfo><FirstName>{0}</FirstName><LastName>{1}</LastName><RequestedClientID>{2}</RequestedClientID></PatientInfo>

显然,我不想手动编写数据,因为每个新字段都需要更新模板。所以,我正在尝试使用XMLSerializer

private string SerializeToString(object data)
{
    if (data == null) return null;
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("", "");

    using (var stringwriter = new System.IO.StringWriter())
    {
        var serializer = new XmlSerializer(data.GetType(), "");
        serializer.Serialize(stringwriter, data, ns);
        return stringwriter.ToString();
    }
}

但我在序列化PatientInfo 时得到下一个结果:

"<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<PatientInfo>\r\n  <FirstName>Andrew</FirstName>\r\n  <LastName>Fox</LastName>\r\n  <Birthday d2p1:nil=\"true\" xmlns:d2p1=\"http://www.w3.org/2001/XMLSchema-instance\" />\r\n  <RequestedClientID>2c547deb-2395-4334-b1b0-58e6562b5843</RequestedClientID>\r\n  <patientId>00000000-0000-0000-0000-000000000000</patientId>\r\n</PatientInfo>"

它不适合 ExecuteWebServiceCommand,给我 (400) 错误请求异常。

那么,我怎样才能正确地将对象序列化为 XML,以使其适合此类代码?或者我怎样才能修改代码来接受这些数据?

【问题讨论】:

  • 至少,我想知道,我如何配置序列化程序以从结果中删除“序言”内容和控制字符。

标签: c# xml web-services wcf


【解决方案1】:

您应该使用XmlWriter,并使用正确的XmlWriterSettings 对其进行实例化,以省略Xml 声明并且在序列化时没有换行符。将其应用于您的代码时,您将得到以下结果:

private string SerializeToString(object data)
{
    if (data == null) return null;
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("", "");

    // what should the XmlWriter do?
    var settings = new XmlWriterSettings
    {
        OmitXmlDeclaration = true,
        NewLineChars = ""
    };

    using (var stringwriter = new System.IO.StringWriter())
    {
       // Use an XmlWriter to wrap the StringWriter
       using(var xmlWriter = XmlWriter.Create(stringwriter, settings))
       {
           var serializer = new XmlSerializer(data.GetType(), "");
           // serialize to the XmlWriter instance
           serializer.Serialize(xmlWriter, data, ns);
           return stringwriter.ToString();
       }
    }
}

这会给我这个结果:

Foo2015-12-19T16:21:48.4009949+01:0000000000-0000-0000-0000-000000000000 00000000-0000-0000-0000-000000000000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多