开发者

DataSet to XDocument Conversion and back

开发者 https://www.devze.com 2023-01-18 00:45 出处:网络
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 i开发者

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 i开发者_运维百科nto 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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消