如何将WCF数据协定转换为XML字符串 [英] How to do I convert a WCF data contract to a XML string

查看:70
本文介绍了如何将WCF数据协定转换为XML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我们可以将WCF数据协定转换为XML字符串,然后将其转换为整数格式。



C#是否有这样的类进行转换?



这是必需的,因为在我们的要求中我们需要传递一个C#数据合同到C ++层。



请告诉我你的意见/建议。



感谢你的帮助。



谢谢,

Sudhakar

解决方案

请参阅我的评论问题。这是数据合同的全部目的,用于在流中序列化数据并从流中恢复数据。 N $



https://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.writeobject%28v=vs.110%29.aspx [< a href =https://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.writeobject%28v=vs.110%29.aspx\"target =_ blanktitle =新窗口> ^ ]。



您可以使用 System.IO.MemoryStream ,编写您的对象to is,并使用 System.IO.StreamReader 将数据读取到字符串。请参阅:

https ://msdn.microsoft.com/en-us/library/system.io.memorystream%28v=vs.110%29.aspx [ ^ ],

< a href =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%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend %28v = vs.110%29.aspx [ ^ ](这是关键),

http://stackoverflow.com/questions/3238748/datacontractserializer-how-can-i-output-the-xml- to-a-string-as-against-to-af [ ^ ]。



至于C ++,这取决于你想要实现的目标。您可以通过P / Invoke使用C ++ DLL。 C ++ / CLI提供了使用混合/模式(管理+非托管)项目的独特可能性,该项目可以兼作.NET程序集和常规本机DLL。然后,您可以将数据协定类包装在本机C ++类中。请参阅: https://en.wikipedia.org/wiki/C%2B%2B/CLI [ ^ ]。



但是 - 请在我对这个问题的评论中看到我的问题 - 也许你只需要带有托管代码的C ++ / CLI,那么这个问题就不存在了。



-SA


嗨谢尔盖,



我在这里添加了更多细节。正如你所建议List有ToArray方法。



我在这里有点困惑。因为在C#中,我有以下代码。



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

< / topicdetails > < / list >





现在如果我使用topicDetails.ToArray();它会将它转换为整数数组,我可以将其转换为C ++ / CLI

然后转换为原生C ++代码



  const  vector< int> & optList 





感谢您的帮助。



谢谢


大家好,



以下代码正在努力满足要求。请看一下。

将Datacontracts列表转换为整数数组。



 < span class =code-keyword> int  [] data; 

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

使用 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();
}


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

解决方案

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


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.

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

const vector<int> &optList 



Appreciate your help.

Thanks


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.

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();
           }


这篇关于如何将WCF数据协定转换为XML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆