这是我的WCF REST OperationContract
[OperationContract(Name = "SaveBasicInfoObjectById")]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "Core/SaveBasicInfoObjectById?ID={ID}")]
List<Response> SaveBasicInfoObjectById(string ID, BasicClass basicClass);
这是我的客户代码
string ID= "123";
MyService.BasicClass basicClass = new MyService.BasicClass();
string sErrorMsg = "";
string sResponseStatus = "";
string URLString = "http://localhost:59133/MyService.svc/Core/SaveBasicInfoObjectById?ID=" + ID;
basicClass.FirstName = txt_Fname.Text.Trim();
basicClass.Lastname = txt_Lname.Text.Trim();
var paramContent = Serialize(basicClass);
CreateWebRequest(URLString, paramContent.ToString());
这是序列化方法
public static string Serialize<T>(T obj)
{
var serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
var ms = new MemoryStream();
serializer.Serialize(ms, obj);
string retVal = Encoding.Default.GetString(ms.ToArray());
ms.Dispose();
return retVal;
}
这是CreateWebRequest方法
private string CreateWebRequest(string endPoint, string paramContent)
{
string result = string.Empty;
byte[] buffer = Encoding.UTF8.GetBytes(paramContent);
var myHttpWebRequest = (HttpWebRequest)WebRequest.Create(endPoint);
myHttpWebRequest.Method = "POST";
myHttpWebRequest.ContentLength = buffer.Length;
myHttpWebRequest.ContentType = "text/xml";
using (var request = myHttpWebRequest.GetRequestStream())
{
request.Write(buffer, 0, buffer.Length);
request.Close();
}
var myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
using (var reader = new StreamReader(myHttpWebResponse.GetResponseStream(), Encoding.UTF8))
{
result = reader.ReadToEnd();
myHttpWebResponse.Close();
}
return result;
}
* 我面临的问题是Object按以下格式序列化,当在服务中遇到断点时,对象值为空,即firstname =“”和lastname =“”* < / em>的
<?xml version="1.0"?>
<BasicClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="0">
<FirstName xmlns="http://tempuri.org/">James</FirstName>
<Lastname xmlns="http://tempuri.org/">Kravis</Lastname>
</BasicClass>
任何人都可以提出建议吗?
我现在已将tempuri命名空间替换为firstname和lastname元素为“”或为空,现在这个工作,客户端的值现在已在服务中收到
以下是序列化方法的更新代码
public string Serialize<T>(T obj)
{
//System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
var serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
var ms = new MemoryStream();
serializer.Serialize(ms, obj);
string retVal = Encoding.Default.GetString(ms.ToArray());
ms.Dispose();
return retVal.Replace("xmlns=\"http://tempuri.org/\"", "");
}
**更新以上代码**
public string Serialize<T>(T obj)
{
var serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
var ms = new MemoryStream();
serializer.Serialize(ms, obj);
string retVal = Encoding.Default.GetString(ms.ToArray());
ms.Dispose();
return retVal;
}
**放弃了替换,找到了停止将默认命名空间添加到元素**
的方法public class BasicClass
{
[XmlElement(Namespace="")]//add namespace as ""
public string Firstname{get;set;}
[XmlElement(Namespace="")]//add namespace as ""
public string Lastname{get;set;}
}
通过执行上述操作,我可以删除默认命名空间,无需替换
<?xml version="1.0"?>
<BasicClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="0">
<FirstName>James</FirstName>
<Lastname>Kravis</Lastname>
</BasicClass>
答案 0 :(得分:1)
默认情况下,WCF使用DataContractSerializer
作为序列化程序,而不是XmlSerializer
。它们生成(并期望)不同的格式,因此您应该更改客户端(使用DataContractSerializer而不是XmlSerializer序列化对象)或服务(将[XmlSerializerFormat]
属性应用于操作或服务合同到让WCF使用该序列化器。)
您也不应该替换“xmlns”声明(Serialize<T>
方法中的最后一行),因为这样做会改变生成的XML的语义。