Click here to Skip to main content
15,346,895 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,

Can we convert a WCF data contract to a XML string , and then can it be converted to integer format.

Does C# has such classes to make the conversion ?

This is required as in our requirement we need to pass a C# data contract to C++ layer.

Please let me know your ideas/suggestions on this.

Appreciate your help.

Thanks,
Sudhakar
Posted
Comments
Andy Lanng 20-Aug-15 6:01am
   
so many ways:
Dataset.ToXml();
POCO Serialization;
http.//mywebServices/WebService.wsdl
It depends on what "data" you want
Member 3975629 26-Aug-15 8:24am
   
Thank you. I will try this out
Sergey Alexandrovich Kryukov 20-Aug-15 11:56am
   
Suggestions? For what? The whole purpose of Data Contract is to write XML (or, say, JSON). What's the problem? Just read the documentation on the serializer class.
As to C++, it is an unrelated issue. It depends on what you want to achieve. By they way, are you sure it's C++, not C++/CLI?
—SA
Member 3975629 26-Aug-15 8:24am
   
Thank you. I will try this out

Please see my comment to the question. It's the whole purpose of Data Contract, to serialize data in stream and restore it from stream. N

https://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.writeobject%28v=vs.110%29.aspx[^].

You can use System.IO.MemoryStream, write your object to is, and use System.IO.StreamReader to read data to string. Please see:
https://msdn.microsoft.com/en-us/library/system.io.memorystream%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend%28v=vs.110%29.aspx[^] (this is the key),
http://stackoverflow.com/questions/3238748/datacontractserializer-how-can-i-output-the-xml-to-a-string-as-opposed-to-a-f[^].

As to C++, it depends on what you want to achieve. You can use C++ DLL via P/Invoke. C++/CLI provides unique possibility to use mixed/mode (manage + unmanaged) project which can double as .NET assembly and regular native DLL. Then you can wrap your data contract classes in native C++ classes. Please see: https://en.wikipedia.org/wiki/C%2B%2B/CLI[^].

But — please see my questions to my comment to the question — perhaps you only need C++/CLI with managed code only, then this problem simply does not exist.

—SA
   
Comments
Member 3975629 26-Aug-15 8:24am
   
Thank you. I will try this out
   
You are welcome. Please do, and don't forget to accept the answer formally. In all cases, your follow-up questions will be welcome.
—SA
Member 3975629 27-Aug-15 7:14am
   
I need to convert the to convert a List of data contracts to an integer array[].
from C# to an Integer array[] in C++/CLI and then to C++ .

I need to convert from C# , List of datacontract to C++ const integer vector &topicList via C++/CLI wrapper.

Could you please let me know how can I achieve this.

Appreciate your help on this.

//topicDetails is a data-contract
CreateSubscriptions<List<topicdetails>(topicDetails);

//Here TopicDetails is a class (data contract) as follows.

public class TopicDetails
{
protected object baseObjectType; //string
protected object topic; //string

public TopicDetails();

[DataMember]
public object BaseObjectType { get; set; }
[DataMember]
public object TopicID { get; set; }

public static TopicDetails CreateTopic<t, mt="">(IComparable<t> objectType, IComparable<mt> objectID);
}


http://www.codeproject.com/Questions/1022559/How-to-convert-a-List-of-DataContracts-to-an-to-an?arn=0
Sergey Alexandrovich Kryukov 27-Aug-15 10:24am
   
List has a ToArray method.
—SA
Member 3975629 28-Aug-15 1:21am
   
Hi Sergey ,

I have added more detail here. As you suggested List has ToArray method.

I am little confused here. Because in C# , I have the following code

subscriptionTopicList.Add(SubscriptionTopic.CreateSubscriptions<List<topicdetails>, string>(topicDetails, "VIEW"));

Now if I use topicDetails.ToArray(); will it convert this to an integer array which I can pass to C++/CLI
and then to native C++ code as const vector<int> &optList ??

Appreciate your help.

Thanks
   
It will convert it the to array of the list element type, of course. It's enough to look at the function profile. Look at the MSDN help pages on System.Collections.Generic.List and System.Array.
—SA
Member 3975629 28-Aug-15 3:42am
   
Hi Sergey ,

I am trying as below .but I am getting an error CS0411: as follows. any idea ?
Here "TopicDetails" is a datacontract class which has string data members.

List<TopicDetails> topicDetails3;
TopicDetails[] arr = topicDetails3.ToArray();

int[] myInts = Array.ConvertAll(arr, int.Parse);

error CS0411: The type arguments for method 'System.Array.ConvertAll<tinput,toutput>(TInput[], System.Converter<tinput,toutput>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
   
Why not reading the MSDN help page on the method?
Here: https://msdn.microsoft.com/en-us/library/kt456a2y%28v=vs.110%29.aspx.
The second argument should be a delegate instance. Such as in:
ConvertAll(Input[], new Converter<>((element) => { return int.Parse(element); }));

However, the question is: why would you have an array of strings in first place. This shows the bad trend to work with strings representing data instead of data itself.

—SA
   
Well, will you accept the answer formally now? I think I explained you all you need.
—SA
Member 3975629 28-Aug-15 1:58am
   
If I use topicDetails.ToArray(); I am getting the error:

Cannot implicitly convert type 'Data.Notif.TopicDetails[]' to 'string[]' .

I think it is because "TopicDetails" is of type data contract "Data.Notif.TopicDetails"

I am not sure how to resolve this error.
Could you please let us know how to resolve this error.
   
Of course. How else? Did you pay attention for my previous comment? Look at System.Array; there is a method for conversion of all elements.
—SA
Hi Sergey ,

I have added more detail here. As you suggested List has ToArray method.

I am little confused here. Because in C# , I have the following code.

C#
obj.CreateSubscriptions<list><topicdetails>, string>(topicDetails, "VIEW");

</topicdetails></list>


Now if I use topicDetails.ToArray(); will it convert this to an integer array which I can pass to C++/CLI
and then to native C++ code as

C++
const vector<int> &optList 


Appreciate your help.

Thanks
   
v3
Hi All,

The following code is working to satisfy the requirement. Please take a look at it.
To convert List of Datacontracts to an integer array.

C#
int[] data;

           var obj = topicDetails;// topicDetails= new List<TopicDetails> { };// List of DataContracts{ BaseObjectType = ":EQUIPMENT", TopicID = "42" };
           var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(List<TopicDetails>));

           using (var stream = new System.IO.MemoryStream())
           {
               serializer.WriteObject(stream, obj);

               data = new int[stream.Length];
               stream.Position = 0;

               for (int i = 0; i < data.Length; i++)
                   data[i] = stream.ReadByte();
           }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900