将xml字符串转换回数据集对象 [英] converting xml string back into dataset object

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

问题描述

我正在将数据集对象转换为xml字符串&然后将其通过套接字传递给某些客户端.

在客户端,当收到字符串时&我将字符串转换回数据集对象&使用该对象作为datagrid的数据源,它给我错误根元素丢失".

将数据集转换为xml字符串的代码:

I am converting the dataset object into xml string & then passing it through socket to some client.

On the client side when the string is received & I convert the string back into dataset object & use that object as datasource for datagrid it gives me error "Root element is missing".

Code to convert datasetinto xml string:

Public Shared Function ConvertObjectToByte(ByVal ds As DataSet) As String

        Dim byReturn As String = Nothing
        Dim sStream As IO.MemoryStream = Nothing

        Dim sw As New StringWriter()
        ds.WriteXml(sStream)
        byReturn = sStream.ToString()
        Return byReturn
    End Function




将字符串转换回数据集对象的代码:




Code to convert string back into dataset object:

ds.ReadXml(New System.IO.StringReader(response))


注意:response是我通过套接字收到的字符串.


任何帮助或建议都将不胜感激.


Note: response is the string that I received through socket.


Any help or suggestion would definitely be appreciated.

推荐答案

您好,不要一再转发您的问题.

您的代码基本上有很多错误.

ds.WriteXml(sStream)将不起作用. WriteXml方法仅适用于FileStreams.

byReturn = sStream.ToString()您想在这里做什么?

您的方法说转换为字节,但它返回一个字符串.你知道你在弄乱这里的人吗?

但是,如果您的目标是从数据集中获取xml字符串,请使用数据集的getXml()函数(string resultString=ds.GetXml();).如果要将字符串转换为字节,请使用byte[] bytes=Encoding.Unicode.GetBytes(resultString);.如果字节返回字符串,则 string resultString1 = Encoding.Unicode.GetString(bytes);.

XML字符串返回到数据集

Hi don''t repost your question again and again.

Your code has many errors basically.

ds.WriteXml(sStream) won''t work. WriteXml method only works with FileStreams.

byReturn = sStream.ToString() what you want to do here?

Your method says convert to byte but it returns a string. Do you understand you are confusing people here?

However if your aim is to get the xml string from data set then use the dataset''s getXml() function (string resultString=ds.GetXml();). If you want to convert that string to bytes then use byte[] bytes=Encoding.Unicode.GetBytes(resultString);. If bytes back to string then string resultString1 = Encoding.Unicode.GetString(bytes);.

XML string back to Dataset

TextReader reader = new StringReader(resultString1);
       DataSet ds1 = new DataSet();
       ds1.ReadXml(reader);



不清楚如何将字节或字符串传输到另一个客户端.这就是你的头痛.如果您传输正确的数据并获取正确的数据,它将正常工作.

祝你好运.



It is unclear how you transfer your byte or string to another client. So that is your headache. if you transfer the right data and get right data it will work fine.

Good luck.


这篇关于将xml字符串转换回数据集对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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