如何反序列化WCF服务中定义的类型的客户端xml

时间:2013-12-17 13:25:47

标签: c# xml wcf

我有一个只有一个父节点的简单xml。我想将这个xml消毒为类类型,这个类是在wcf服务中定义的。该类是使用xsd.exe从xml生成的。

目的是 - 以类对象形式而不是xml将信息发送到wcf服务。

但是当我在客户端使用该类时,所有属性名称都会更改并附加一些后缀。并且所有属性都设置为null。

我的XML看起来像 -

<NafDetails xmlns="http://schemas.datacontract.org/2004/07/....Service.Contract">
  <NafInfo_NafNumber></NafInfo_NafNumber>
   <NafInfo_CFAcctNum></NafInfo_CFAcctNum>
   <NafInfo_RepNum></NafInfo_RepNum>

  <NafInfo_FASIRIA>0</NafInfo_FASIRIA>
  <NafInfo_OutsideRIA>0</NafInfo_OutsideRIA>
</NafDetails>

我的反序列化方法看起来像

    public NafDetails Deserialize(string laserEncodedXML)
    {
        try
        {
            XDocument xDoc = XDocument.Parse(laserEncodedXML);
            var xmlSerializer = new XmlSerializer(typeof(NafDetails), "http://schemas.datacontract.org/2004/07/....Service.Contract");               
            NetDataContractSerializer obj = new NetDataContractSerializer();
            using (var xReader = xDoc.CreateReader())
            {
               //return (NafDetails)obj.Deserialize(xReader.);
               return (NafDetails)xmlSerializer.Deserialize(xReader);
                //return (NafDetails)dcs.ReadObject(xReader);
            }
        }
        catch (SerializationException)
        {
            throw;
        }
        catch (Exception)
        {
            throw;
        }
    }  

我在WCF中定义的类就像 -

    [Serializable, XmlRoot("NafDetails")]
    [DebuggerStepThrough]
    [XmlType(AnonymousType = true)] 
    public class NafDetails
    {
        [XmlElement("NafInfo_NafNumber")]
        public string NafInfo_NafNumber { get; set; }
        [XmlElement("NafInfo_CFAcctNum")]
        public string NafInfo_CFAcctNum { get; set; }
        [XmlElement("NafInfo_RepNum")]
        public string NafInfo_RepNum { get; set; }
        [XmlElement("NafInfo_FASIRIA")]
        public bool NafInfo_FASIRIA { get; set; }
        [XmlElement("NafInfo_OutsideRIA")]
        public bool NafInfo_OutsideRIA { get; set; }
        [XmlElement("NafInfo_DirectBusiness")]
        public bool NafInfo_DirectBusiness { get; set; }
        [XmlElement("NafInfo_CF_Pershing")]
        public bool NafInfo_CF_Pershing { get; set; }
        [XmlElement("NafInfo_CF_JPMorgan")]
        public bool NafInfo_CF_JPMorgan { get; set; }
        [XmlElement("NafInfo_DVP")]
        public bool NafInfo_DVP { get; set; }
    }

请指导我完成这项工作。谢谢。

1 个答案:

答案 0 :(得分:0)

你可以试试下面的东西。

    string xml = "<NafDetails>"+
                  "<NafInfo_NafNumber>Test</NafInfo_NafNumber>"+
                   "<NafInfo_CFAcctNum>New</NafInfo_CFAcctNum>"+
                   "<NafInfo_RepNum>Demo</NafInfo_RepNum>"+
                     "<NafInfo_FASIRIA>0</NafInfo_FASIRIA>"+
                  "<NafInfo_OutsideRIA>0</NafInfo_OutsideRIA>"+
                "</NafDetails>";

    NafDetails nafdetail;
    XmlSerializer serializer = new XmlSerializer(typeof(NafDetails));
    nafdetail = (NafDetails) serializer.Deserialize(XmlReader.Create(new StringReader(xml)));

由于 迪普