简体   繁体   中英

Multiple XML file insert into SQL

I have a large XML file with around 70-100 tags including lists(child nodes) etc. I have an Oracle database setup with all of the tables matched to the tag names. I was wondering what would be the quickest way of insert all data from the XML into the Oracle database i have. I have written this method below, however writing this for 80 tables will become very tedious:

   public ActionResult writexmldata()
    {
        var file = System.IO.Directory.GetFiles("C:\\Workspace\\CPTStaging", "*.xml");
        CPTEntities db;

        foreach (var xmldoc in file)
        {
            db = new CPTEntities();
            XDocument xdoc = XDocument.Load(xmldoc);
            XNamespace ns = "http://www.example.org/genericClientProfile";

            CPTPROFILE doc = new CPTPROFILE();
            db.CPTPROFILEs.AddObject(doc);
            db.SaveChanges();

            var header = xdoc.Descendants(ns + "header").Single();
            var cprofile = xdoc.Descendants(ns + "clientProfile").Single();
            var profadv = xdoc.Descendants(ns + "section").Single();

            H_HEADER head = new H_HEADER();

            head.SERVICEID = (string)header.Element(ns + "serviceId");
            head.VERSIONID = (decimal)header.Element(ns + "versionId");
            head.BRANDCODE = (string)header.Element(ns + "brandCode");
            head.CREATIONTIME = (DateTime)header.Element(ns + "creationTime");

            db.H_HEADER.AddObject(head);

            CP_PROFESSIONALADVISERS advisers = new CP_PROFESSIONALADVISERS();
            advisers.SUBCOMMENTWILLS = (string)profadv.Element(ns + "subCommentWills");

            db.CP_PROFESSIONALADVISERS.AddObject(advisers);

            db.SaveChanges(System.Data.Objects.SaveOptions.None);
        }

        return View("Index");
    }

我将编写代码生成器,并使用您作为模板编写的方法,将对系统表all_tab_columnsall_tables运行查询,并将生成70-80个类似方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM