开发者

how to write datasets to xml in asp.net

开发者 https://www.devze.com 2023-01-25 06:24 出处:网络
I have just started learning asp.net with c# and want to 开发者_运维问答know how to write datasets to XML using asp.net.

I have just started learning asp.net with c# and want to 开发者_运维问答know how to write datasets to XML using asp.net. Can anyone provide me any example or reference so that I can move ahead.

Thanks in Advance

Anu Sharma


The WriteXml method will save the entire dataset to an xml file or stream.

myDataset.WriteXml("dataset.xml", XmlWriteMode.WriteSchema);


Create one empty data dataset with database name, so that this name will reflect to your converted xml document

DataSet ds = new DataSet();
ds.DataSetName = "ds";

Add your data (data tables) to this dataset with name

data.TableName = "tb";
ds.Tables.Add(data);

Create one XmlDocument & load data string into it

using (MemoryStream memoryStream = new MemoryStream())
{
    using (TextWriter streamWriter = new StreamWriter(memoryStream))
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataSet));
        xmlSerializer.Serialize(streamWriter, ds);
        result = Encoding.UTF8.GetString(memoryStream.ToArray());
    }
}

XmlDocument _doc = new XmlDocument();
_doc.LoadXml(result);
0

精彩评论

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