我们如何将IList对象转换为XML并将IList对象转换为JSON [英] How can we convert IList object to XML and Also IList object to JSON

查看:99
本文介绍了我们如何将IList对象转换为XML并将IList对象转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们如何将1)IList对象转换为XML,以及2)将IList对象转换为JSON
有人可以告诉我如何完成这些任务吗?

实际问题是:

我所做的是制作一个WCF服务,该服务从数据库表中读取数据.然后我要做的是,我使用LINQ to SQL,并进入foreach循环,在该循环中,我将所有记录存储在IList对象中.
假设我在finalObject中获得了具有事件信息的Ilist对象.现在我要做的就是将其更改为XML,因为我想向客户端提供XML格式(也是JSON格式).

例子是值得赞赏的.这将使我成为C#的新手变得更容易

问候
John Smith

Hi,

How can we convert 1)IList object to XML and 2)Also IList object to JSON
Does anybody can tell me how to do these tasks?

The Actual problem is:

what I have done is made a WCF Service whichs reads the data from database table. then what i do, I use LINQ to SQL and comes into the foreach loop where I store all the records in a IList Object.
suppose I have got the Ilist Ojbect having events information in finalObject. what I have to do now is change it to XML becuase I want to provide the XML format (also JSON format) to the Clients.

Example is appreciated. it will make the things easier for me being a novice in the C#

regards
John Smith

推荐答案

最简单的方法可能是使存储在IList实例中的对象成为 ^ ](即使用 DataContract [< ^ ]和DataMember [ DataContractSerializer [DataContractJsonSerializer [
The easiest way to do this is probably to make the objects stored in the IList instances to be data contracts[^] (i.e. use the DataContract[^] and DataMember[^] attributes). Then serialize the lists with DataContractSerializer[^] for XML or DataContractJsonSerializer[^] for JSON.


该问题未定义. (我不是说问题不清楚,我是说问题没有定义,不适,请见下文.)

您不能将一个转换"为另一个.这个不成立.为了使它具有某种意义,您还需要一个输入:元数据,该数据显示要在什么数据集和什么映射上映射什么数据集.实现IList的对象的实例将保存对象和任何性质.您需要将其限制为某些类型的对象.您需要将显示如何映射事物的数据(元数据).没有这些,问题就变得模棱两可.

在某些情况下,这并不重要,您只需要以某种方式存储数据,然后再一对一地还原它即可.这说得通.在这种情况下,请使用数据合同.这很容易,从这里开始:
http://msdn.microsoft.com/en-us/library /ms733127.aspx [ ^ ].


如果要使用JSON,请查看以下代码示例: http://msdn.microsoft.com/en -us/library/bb412179.aspx [ ^ ].
对于数据成员,您可以使用很多类型,包括IList,如下所示:

The problem is not defined. (I don''t say the Question is not clear, I say the problem is not defined, ill-posed, pleas see below.)

You cannot "convert" one into another; it makes no sense. To bring some sense into it, you need one more input: meta-data showing what set of data you want to map on what set of data and what the mapping is. The instance of the object implementing IList will hold object and any nature. You need to limit it to object of certain types. You need data (meta-data) which will show how to map thing. Without that, the problem is ambiquous.

In some cases, it does not matter, you need just to store data somehow and later restore it one-to-one. It makes sense. In this case, use Data Contract. This would be easy, start from here: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].


If you want to use JSON, look at this code sample: http://msdn.microsoft.com/en-us/library/bb412179.aspx[^].
For the data member you can use very many types, including IList, like this:

[DataContract]
class MyClass {
   [DataMember]
   IList MyContainer;
   //...
}



—SA



—SA


这篇关于我们如何将IList对象转换为XML并将IList对象转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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