首页 > 代码库 > 如何将XML文件写入数据库

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