2014-12-05 83 views
0

我已經構建了一個運行良好的web服務,除了我試圖刪除xsi和xsd命名空間。從wcf中刪除不必要的xsi和xsd命名空間

我已經看到了很多環節的表現,我不得不使用一個自定義序列是這樣的:

XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); 
namespaces.Add(string.Empty, string.Empty); 

但我沒有找到一種方法,在我的代碼來實現這一點。這裏是我的代碼:

[ServiceContract, XmlSerializerFormat] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class MyUser 
{ 
    [OperationContract] 
    [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "getUserInfo?token={userId}", BodyStyle = WebMessageBodyStyle.Bare)] 
    public PersonnResponse ValidateToken(string userId) 
    { 
     var response = new PersonnResponse(); 
     response.userId = userId; 
     response.firstName = myOtherServiceGetFirstName(userId); 
     response.lastName = myOtherServiceGetLastName(userId); 
     return response; 
    } 

[DataContract(Name = "person", Namespace = "")] 
public class PersonnResponse 
{ 
    [DataMember(Name = "userId", EmitDefaultValue = false)] 
    public string userId { get; set; } 

    [DataMember(Name = "firstName", EmitDefaultValue = false)] 
    public string firstName { get; set; } 

    [DataMember(Name = "lastName", EmitDefaultValue = false)] 
    public string lastName { get; set; } 
} 
+1

爲什麼:

[DataContract(Name = "person", Namespace = "")] public class PersonnResponse:IXmlSerializable { ... public XmlSchema GetSchema() { return null; } public void ReadXml (XmlReader reader) { var xd = XDocument.Load(reader); firstName = xd.Descendants().First (x => x.Name.LocalName == "firstName").Value; lastName = xd.Descendants().First (x => x.Name.LocalName == "lastName").Value; userId = xd.Descendants().First (x => x.Name.LocalName == "userId").Value; } public void WriteXml(XmlWriter writer){ writer.WriteElementString("userId", userId); writer.WriteElementString("firstName", firstName); writer.WriteElementString("lastName", lastName); } } public class Test { static void Main() { Test t = new Test(); t.Serialize(); } private void Serialize() { // Create an instance of the class, and an // instance of the XmlSerializer to serialize it. var pers = new PersonnResponse(){ firstName="Call Me", lastName="Heisenberg", userId="Id"}; XmlSerializer ser = new XmlSerializer(typeof(PersonnResponse)); StringWriter tw = new StringWriter(); ser.Serialize(tw,pers); Console.WriteLine(tw.ToString()); //Deserialize from XML string var sw = new StringReader(tw.ToString()); var NewPerson = ser.Deserialize(sw); } } 

你會用這樣的XML結束你想擺脫這些命名空間? – 2014-12-08 22:43:47

+0

猜怎麼着?來自另一家公司的客戶不喜歡他們! – 2014-12-09 02:36:14

+0

「不喜歡他們」?您可能需要輕輕問客戶同樣的問題。當然,你必須在「客戶發言」中做到這一點。例如,對客戶說,「我們將努力擺脫命名空間,因爲你發現它們不整潔」。然後客戶會說,「哦,是的,這是正確的」,或者「不,這不是原因 - 我們有這個腳本無法解析名稱空間」。 – 2014-12-09 02:44:24

回答

1

爲了讓你在找什麼,你應該實現IXmlSerializable

<?xml version="1.0" encoding="utf-16"?> 
<PersonnResponse> 
    <userId>Id</userId> 
    <firstName>Call Me</firstName> 
    <lastName>Heisenberg</lastName> 
</PersonnResponse> 
+0

我可以理解這一點。我想知道我到底在哪裏告訴我的WCF項目將該文檔序列化並將其發回。 – 2016-10-24 19:48:28