将DataTable转换为Xml,以及互转换

前端之家收集整理的这篇文章主要介绍了将DataTable转换为Xml,以及互转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
        private string ConvertDataTableToXML(DataSet ods)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                //XmlReader xx=new XmlRead
                using (XmlTextWriter writer = new XmlTextWriter(stream,Encoding.Default))
                {
                    try
                    {                      
                        ods.WriteXml(writer,XmlWriteMode.WriteSchema);
                        byte[] bytes = new byte[(int)stream.Length];
                        stream.Seek(0,SeekOrigin.Begin);
                        stream.Read(bytes,(int)stream.Length);
                        return System.Text.Encoding.Default.GetString(bytes);
                    }
                    catch(Exception err)
                    {
                        return err.Message;
                    }
                }
            }
        }





        private DataSet ConvertXMLToDataSet(string data)
        {
            using (StringReader stream = new StringReader(data))
            {
                using (XmlReader reader = XmlReader.Create(stream))
                {
                    try
                    {
                        DataSet ds = new DataSet();
                        ds.ReadXml(reader);
                        if (ds.Tables.Count > 0)
                            return ds;
                        else
                            return null;
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
        }
原文链接:https://www.f2er.com/xml/299440.html

猜你在找的XML相关文章