开发者

Translate Service Call Object Back to the Original Object

I have an object exposed through a web service that is consumed by another system. That system also uses that same WSDL to return back an object on demand to us. I was wondering if it was possible to take that same object and relay it back to a the original object?

I tried to do the following and it wouldn't actually cast it back with the object populated... Any help would be great!

Thank you!

Note The method code listed below was based off of 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开发者_JAVA技巧>
    /// <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;
    }


When generating the proxy from WSDL there is an option called "/sharetypes" which should solve this problem

You can use it from the command line tools or in the options area of the "Add Web Service" dialogs

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜