我有喜欢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