将服务调用对象转换回原始对象

时间:2010-01-18 12:57:18

标签: c# .net web-services serialization

我有一个通过另一个系统使用的Web服务公开的对象。该系统还使用相同的WSDL将请求对象返回给我们。我想知道是否有可能采取相同的对象并将其转发回原始对象?

我尝试执行以下操作,但实际上并没有将对象填充回来...任何帮助都会很棒!

谢谢!

注意下面列出的方法代码基于http://www.dotnetjohn.com/articles.aspx?articleid=173

    public void ConvertBack()
    {
        ThirdParty.Animal animal;
        using (var svc = new ThirdPartySoapClient())
            thirdPartyAnimal = svc.GetAnimal("Identifier");

        var xml = SerializeObject(thirdPartyAnimal, typeof(ThirdParty.Animal));

        var originalAnimal = (OriginalNamespace.Animal)DeserializeObject(xml, typeof(OrginalNamespace.Animal));

        Assert.AreEqual(originalAnimal.Name, animal.Name);
    }


    /// <summary>
    /// Method to convert a custom Object to XML string
    /// </summary>
    /// <param name="pObject">Object that is to be serialized to XML</param>
    /// <param name="typeOfObject">typeof() object that is being passed to be serialized to XML</param>
    /// <returns>XML string</returns>
    public string SerializeObject(object pObject, Type typeOfObject)
    {
        try
        {
            var memoryStream = new MemoryStream();
            var xs = new XmlSerializer(typeOfObject);
            var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

            xs.Serialize(xmlTextWriter, pObject);
            memoryStream = (MemoryStream) xmlTextWriter.BaseStream;
            var xmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
            return xmlizedString;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            return null;
        }
    }

    /// <summary>
    /// Method to reconstruct an Object from XML string
    /// </summary>
    /// <param name="pXmlizedString">XML To Be Converted to an Object</param>
    /// <param name="typeOfObject">typeof() object that is being passed to be serialized to XML</param>
    /// <returns></returns>
    public object DeserializeObject(string pXmlizedString, Type typeOfObject)
    {
        var xs = new XmlSerializer(typeOfObject);
        var memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
        new XmlTextWriter(memoryStream, Encoding.UTF8);
        return xs.Deserialize(memoryStream);
    }

    /// <summary>
    /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
    /// </summary>
    /// <param name="characters">Unicode Byte Array to be converted to String</param>
    /// <returns>String converted from Unicode Byte Array</returns>
    private static string UTF8ByteArrayToString(Byte[] characters)
    {
        var encoding = new UTF8Encoding();
        var constructedString = encoding.GetString(characters);
        return (constructedString);
    }

    /// <summary>
    /// Converts the String to UTF8 Byte array and is used in De serialization
    /// </summary>
    /// <param name="pXmlString"></param>
    /// <returns></returns>
    private static Byte[] StringToUTF8ByteArray(string pXmlString)
    {
        var encoding = new UTF8Encoding();
        var byteArray = encoding.GetBytes(pXmlString);
        return byteArray;
    }

1 个答案:

答案 0 :(得分:0)

从WSDL生成代理时,有一个名为"/sharetypes"的选项可以解决这个问题

您可以从命令行工具或“添加Web服务”对话框的选项区域中使用它