这是我的 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;
}
*我面临的问题是对象以以下格式序列化,并且当在服务处命中断点时,对象值为空,即 firstname = "" 和 lastname = "" *
<?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>