Click here to Skip to main content
15,867,834 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
 
Share this answer
 
Comments
Member 3975629 26-Aug-15 8:24am    
Thank you. I will try this out
Sergey Alexandrovich Kryukov 26-Aug-15 9:34am    
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
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
 
Share this answer
 
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();
           }
 
Share this answer
 

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