我有喜歡XML反序列化給空WCF對象
<?xml version="1.0"?>
<FullServiceAddressCorrectionDelivery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AuthenticationInfo xmlns="http://www.usps.com/postalone/services/UserAuthenticationSchema">
<UserId xmlns="">FAPushService</UserId>
<UserPassword xmlns="">Password4Now</UserPassword>
</AuthenticationInfo>
</FullServiceAddressCorrectionDelivery>
一個XML字符串,以便與類節點圖,我有在WCF類結構像
[DataContract]
[Serializable]
public class FullServiceAddressCorrectionDelivery
{
[XmlElement("AuthenticationInfo", Namespace = "http://www.usps.com/postalone/services/UserAuthenticationSchema")]
[DataMember]
public AuthenticationInfo AuthenticationInfo { get; set; }
}
[DataContract]
[Serializable]
public class AuthenticationInfo
{
[DataMember]
[XmlElement("UserId", Namespace = "")]
public string UserId { get; set; }
[DataMember]
[XmlElement("UserPassword", Namespace = "")]
public string UserPassword { get; set; }
}
對於反序列化,我用xmlserializer去反序列化對象
XmlSerializer xs = new XmlSerializer(typeof(FullServiceAddressCorrectionDelivery));
var result = (FullServiceAddressCorrectionDelivery)xs.Deserialize(stream);
這個方法返回了一個NULL FullServiceAddres sCorrectionDelivery對象.. 但是當我使用的DataContractSerializer ..像
DataContractSerializer xs = new DataContractSerializer(typeof(FullServiceAddressCorrectionDelivery));
var result = (FullServiceAddressCorrectionDelivery)xs.ReadObject(stream);
然後下面的異常出來..
錯誤在第2行位置46期望來自名字空間元素 'FullServiceAddressCorrectionDelivery' 的「http:// schemas.datacontract.org/2004/07/WcfService1'..遇到名爲'FullServiceAddressCorrectionDelivery',名稱空間''的'Element'。
我堅持這個... 我以某種方式與RENE(StackOverFlow成員)的幫助成功反序列化,如果類在同一個項目..但是當我將它們轉換爲WCF Datacontracts ..我碰到問題.....請指導我在哪裏做錯了這裏...
從哪裏得到XML?這是從您的服務返回? –
nops ...它是用戶輸入爲一個字符串,我必須從這個XML字符串填充一個WCF datacontract對象 – Hassam
請幫助..我真的需要做這件事情.... – Hassam