2013-10-10 34 views
0

我開發了WCF客戶端。我的客戶應該驗證傳入的消息。WCF消息反序列化後丟失了前綴的定義

其中一個消息具有以下結構:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <SOAP-ENV:Header>...</SOAP-ENV:Header> 
    <SOAP-ENV:Body> 
    <OpDescriptionResponse> 
     <Field Name="DateTime" Type="xsd:dateTime"> 
    </OpDescriptionResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

在這種情況下,客戶端應該驗證:字段「日期時間」的類型是「日期時間」,從名稱空間「http://www.w3.org/2001/XMLSchema」。

此響應在包含XmlElement數組的結構中反序列化。

但我有一個問題:消息反序列化後,我收到了相應的變量,包含所有的字段節點,我無法確定前綴「xsd」的值,即如果我在接收字段節點時接受任何類型的XMLElement元素並調用element.GetNamespaceOfPrefix(「xsd」)我得到空字符串作爲結果。

如何獲得前綴的定義在反序列化後保存?

請幫我解決這個問題。

回答

0

爲了影響命名空間/前綴,您需要使用XmlSerializerNamespaces。

以下代碼提供一個粗略的參考:

XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); 
namespaces.Add("prefixHere", "http://namespace.here/"); 
XmlSerializer tempSerializer = new XmlSerializer(messageObject.GetType()); 
tempSerializer.Serialize(Console.Out, messageObject, namespaces); 

問候,