您的位置:首页 > 博客中心 > 数据库 >

如何将XML文件写入数据库

时间:2022-03-13 22:54

将xml文件转成string

        public string XMLDocumentToString(XmlDocument doc)
        {
            MemoryStream stream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(stream, null);
            writer.Formatting = Formatting.Indented;
            doc.Save(writer); //转换

            StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
            stream.Position = 0;
            string xmlString = sr.ReadToEnd();
            sr.Close();
            stream.Close();

            return xmlString;
        } 

 将string转成DataTable

        private DataSet GetXmlImages(string subFolder)
        {
            string[] xmlFiles = Directory.GetFiles(SourceDirectory + "\\" + subFolder, "*.xml", SearchOption.AllDirectories);
            if (xmlFiles.Length > 0)
            {
                string xmlFilePath = xmlFiles[0];
                XmlDocument doc = new XmlDocument();
                doc.Load(xmlFilePath);
                string xmlfile = XMLDocumentToString(doc);
                DataSet xmlInfo = new DataSet(); ;
                xmlInfo = ConvertXMLToDataSet(xmlfile);
                return xmlInfo;
            }
            return null;
        }

将DataTable写入数据库

如何将XML文件写入数据库,布布扣,bubuko.com

热门排行

今日推荐

热门手游