如何将XML文件写入数据库

将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写入数据库

posted @ 2014-08-21 11:32  emmaKang  阅读(1711)  评论(0编辑  收藏  举报