0

I'm using a web service that gets some phone numbers in php array as below :

$to=array("phoneNumbers"=>array('a','b'));

I need to Use this web service in C# but I don't know how to Convert the $to variable to C# I already try

new string[] {"a","b"}
new string[] {"phoneNumbers","a","b"}
new string[][] {"phoneNumbers",new string[]{"a","b"}}

None of them is working

This is how I call the web service in PHP

//call soap client
$soap=new SoapClient("http://www.zamanak.ir/api/soap-v3?wsdl");

//get clientId and client Secret from Zamanak.ir
$soap->clientId="";
$soap->clientSecret="";

//add your username and password
$soap->username="";
$soap->password="";

//get authentication
$array = $soap->authenticate($soap->clientId,$soap->clientSecret,$soap->username,$soap->password);
$uid =$array['uid'];
$token =$array['token'];
$to=array("phoneNumbers"=>array('09121232131','091212313221','091254545545'));
$r=$soap->calculateCost( $soap->clientId, $soap->clientSecret, $uid, $token,'iran',$to, '3880', $repeatTotal = 1);
var_export($r);
die;
3
  • What kind of webservice is it and how do you call it at the moment? Commented May 10, 2015 at 8:27
  • Why dont You sens JSON ? Commented May 10, 2015 at 8:37
  • Is the array you are using currently more than one level in depth? If so, the dictionary solution will not do any good ;-) Commented May 10, 2015 at 22:08

1 Answer 1

1

I would say it is crating an associative array, so in C# it should be something like this:

Dictionary<String, String[]> array = new Dictionary<String, String[]>
{
    { "phoneNumbers", new String[]{"a","b"} }
};

then you access it like

array["phoneNumbers"][0] //returns "a"

If you need to use it in a web service you will need a serializable generic dictionary like the one mentioned here.

so my example web service would look something like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Xml.Serialization;

namespace WcfService1
{
    [XmlRoot("dictionary")]
    public class SerializableDictionary<TKey, TValue>
        : Dictionary<TKey, TValue>, IXmlSerializable
    {
        #region IXmlSerializable Members
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

            bool wasEmpty = reader.IsEmptyElement;
            reader.Read();

            if (wasEmpty)
                return;

            while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
            {
                reader.ReadStartElement("item");

                reader.ReadStartElement("key");
                TKey key = (TKey)keySerializer.Deserialize(reader);
                reader.ReadEndElement();

                reader.ReadStartElement("value");
                TValue value = (TValue)valueSerializer.Deserialize(reader);
                reader.ReadEndElement();

                this.Add(key, value);

                reader.ReadEndElement();
                reader.MoveToContent();
            }
            reader.ReadEndElement();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

            foreach (TKey key in this.Keys)
            {
                writer.WriteStartElement("item");

                writer.WriteStartElement("key");
                keySerializer.Serialize(writer, key);
                writer.WriteEndElement();

                writer.WriteStartElement("value");
                TValue value = this[key];
                valueSerializer.Serialize(writer, value);
                writer.WriteEndElement();

                writer.WriteEndElement();
            }
        }
        #endregion
    }

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string calculateCost(SerializableDictionary<String, String[]> to);        
    }

    public class Service1 : IService1
    {
        public string calculateCost(SerializableDictionary<String, String[]> to)
        {
            StringBuilder output = new StringBuilder();
            foreach (KeyValuePair<String, String[]> item in to)
            {
                output.AppendLine(string.Format("{0} => {1}", item.Key, String.Join(",", item.Value)));
            }
            return output.ToString();
        }      
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I try this but it didn't work! I got this Error : "There was an error generating the XML document."
Thanks. Can you show me how to generate this array using xml? It might solve my problem.
I think so, but how can I create a SerializableDictionary like that? Can you please tell me the exact code?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.