本文介绍了DataSet到XDocument转换并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个应用程序,我需要将DataSet转换为XDOcument在中间层(XDOcument比XmlDocument更轻的传输),然后将XDocument转换回DataSet在前端。



我无法找出有效的方法。到目前为止,我将DataSet转换为XMlDocumenmt,然后转换为XDocument和反之亦然。有没有更好的方法?



谢谢。

解决方案

可序列化这可能比XDocument更容易运输。

  string xmlString; 

System.Xml.Serialization.XmlSerializer oSerializer = new System.Xml.Serialization.XmlSerializer(typeof(DataSet));

DataSet ds = new DataSet();
StringBuilder sb = new StringBuilder();

//一边
使用(StringWriter sw = new StringWriter(sb))
{
oSerializer.Serialize(sw,ds);
xmlString = sb.ToString();
}

//其他边
使用(StringReader sr = new StringReader(xmlString))
{
ds =(DataSet)oSerializer.Deserialize sr);
}


I am working on an application where I need to convert a DataSet into XDOcument in the Middle Tier ( XDOcument is much lighter to transport than XmlDocument) and then covert the XDocument back into DataSet at the Front end.

I am not able to figure out an efficient way of doing this. As of now I am converting the DataSet to XMlDocumenmt and then to XDocument and viceversa. Is there a better way?

Thanks.

解决方案

DataSets are serializable. That would probably be easier to transport than XDocument.

string xmlString;

System.Xml.Serialization.XmlSerializer oSerializer = new System.Xml.Serialization.XmlSerializer(typeof(DataSet));

DataSet ds = new DataSet();
StringBuilder sb = new StringBuilder();

//One side
using (StringWriter sw = new StringWriter(sb))
{
    oSerializer.Serialize(sw, ds);
    xmlString = sb.ToString();
}

//Other side
using (StringReader sr = new StringReader(xmlString))
{
    ds = (DataSet)oSerializer.Deserialize(sr);
}

这篇关于DataSet到XDocument转换并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 03:24