2012-05-02 4 views
0

私はXMLデータベースをSQLデータベースに入れる必要があります。私はXMLにLINQを使用しています。私はXMLから必要なデータを取得します。LinqをXMLに使用してXMLをSQLにインポートすると、より効率的な方法がありますか?

Linqを使って変数をXMLにマッピングし、それらの変数を保存することで、多くの時間を無駄にしているようです。私はこれらのステップを組み合わせることができるかどうか疑問に思っています。

これは例示的なクエリです。私はこれを何回も同様のデータで実行します。

何とかLINQ to XMLを作成してLINQの代わりに直接LINQオブジェクトを作成すると、舞台裏で独自のオブジェクトを作成できます。

ここに私のコードです。代わりにあなたが直接PCIクラスを作成することができますし、すべてのものをコピーする必要はない匿名型に突出した

 HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(newURI); 
     request2.AllowAutoRedirect = true; 
     request2.PreAuthenticate = true; 
     request2.Credentials = credentialCache; 
     request2.AutomaticDecompression = DecompressionMethods.GZip; 
     request2.Method = "GET"; 

     HttpWebResponse response2 = null; 
     try 
     { 
      response2 = (HttpWebResponse)request2.GetResponse(); 
      HttpStatusCode statusCode2 = response2.StatusCode; 

      using (Stream responseData2 = response2.GetResponseStream()) 
      { 
       // TODO: Handle HTTP status code and response data here 

       XDocument userInfo = XDocument.Load(responseData2); 
       XNamespace userInfoNamespace = "http://www.digitalmeasures.com/schema/data"; 
       XNamespace dmdNamespace = "http://www.digitalmeasures.com/schema/data-metadata"; 

       var Records = from Record in userInfo.Descendants(userInfoNamespace + "Record") 
           select new 
           { 
            college = Record.Element(dmdNamespace + "IndexEntry").Attribute("entryKey").Value, 
            department = Record.Element(dmdNamespace + "IndexEntry").ElementsAfterSelf(dmdNamespace + "IndexEntry").FirstOrDefault().IsEmpty ? Record.Element(dmdNamespace + "IndexEntry").ElementsAfterSelf(dmdNamespace + "IndexEntry").FirstOrDefault().Attribute("entryKey").Value : "blank", 
            prefix = (string)Record.Descendants(userInfoNamespace + "PREFIX").SingleOrDefault(), 
            fname = (string)Record.Descendants(userInfoNamespace + "FNAME").SingleOrDefault(), 
            pfname = (string)Record.Descendants(userInfoNamespace + "PFNAME").SingleOrDefault(), 
            mname = (string)Record.Descendants(userInfoNamespace + "MNAME").SingleOrDefault(), 
            lname = (string)Record.Descendants(userInfoNamespace + "LNAME").SingleOrDefault(), 
            suffix = (string)Record.Descendants(userInfoNamespace + "SUFFIX").SingleOrDefault(), 
            alt_name = (string)Record.Descendants(userInfoNamespace + "ALT_NAME").SingleOrDefault(), 
            endpos = (string)Record.Descendants(userInfoNamespace + "ENDPOS").SingleOrDefault(), 
            email = (string)Record.Descendants(userInfoNamespace + "EMAIL").SingleOrDefault(), 
            building = (string)Record.Descendants(userInfoNamespace + "BUILDING").SingleOrDefault(), 
            ophone1 = (string)Record.Descendants(userInfoNamespace + "OPHONE1").SingleOrDefault(), 
            ophone2 = (string)Record.Descendants(userInfoNamespace + "OPHONE2").SingleOrDefault(), 
            ophone3 = (string)Record.Descendants(userInfoNamespace + "OPHONE3").SingleOrDefault(), 
            dphone1 = (string)Record.Descendants(userInfoNamespace + "DPHONE1").SingleOrDefault(), 
            dphone2 = (string)Record.Descendants(userInfoNamespace + "DPHONE2").SingleOrDefault(), 
            dphone3 = (string)Record.Descendants(userInfoNamespace + "DPHONE3").SingleOrDefault(), 
            fax1 = (string)Record.Descendants(userInfoNamespace + "FAX1").SingleOrDefault(), 
            fax2 = (string)Record.Descendants(userInfoNamespace + "FAX2").SingleOrDefault(), 
            fax3 = (string)Record.Descendants(userInfoNamespace + "FAX3").SingleOrDefault(), 
            website = (string)Record.Descendants(userInfoNamespace + "WEBSITE").SingleOrDefault(), 
            gender = (string)Record.Descendants(userInfoNamespace + "GENDER").SingleOrDefault(), 
            ethnicity = (string)Record.Descendants(userInfoNamespace + "ETHNICITY").SingleOrDefault(), 
            citizen = (string)Record.Descendants(userInfoNamespace + "CITIZEN").SingleOrDefault(), 
            bio = (string)Record.Descendants(userInfoNamespace + "BIO").SingleOrDefault(), 
            teaching_interests = (string)Record.Descendants(userInfoNamespace + "TEACHING_INTERESTS").SingleOrDefault(), 
            research_interests = (string)Record.Descendants(userInfoNamespace + "RESEARCHINTERETS").SingleOrDefault(), 
            expertise = (string)Record.Descendants(userInfoNamespace + "EXPERTISE").SingleOrDefault(), 
            upload_photo = (string)Record.Descendants(userInfoNamespace + "UPLOAD_PHOTO").SingleOrDefault(), 
            upload_cv = (string)Record.Descendants(userInfoNamespace + "UPLOAD_CV").SingleOrDefault(), 
           }; 

       foreach (var recordItem in Records) 
       { 
        PCI a = new PCI(); 
        a.COLLEGE = recordItem.college; 
        a.DEPARTMENT = recordItem.department; 
        a.PREFIX = recordItem.prefix; 
        a.FNAME = recordItem.fname; 
        a.PFNAME = recordItem.pfname; 
        a.MNAME = recordItem.mname; 
        a.LNAME = recordItem.lname; 
        a.SUFFIX = recordItem.suffix; 
        a.ALT_NAME = recordItem.alt_name; 
        a.ENDPOS = recordItem.endpos; 
        a.EMAIL = recordItem.email; 
        a.BUILDING = recordItem.building; 
        a.OPHONE1 = recordItem.ophone1; 
        a.OPHONE2 = recordItem.ophone2; 
        a.OPHONE3 = recordItem.ophone3; 
        a.DPHONE1 = recordItem.dphone1; 
        a.DPHONE2 = recordItem.dphone2; 
        a.DPHONE3 = recordItem.dphone3; 
        a.FAX1 = recordItem.fax1; 
        a.FAX2 = recordItem.fax2; 
        a.FAX3 = recordItem.fax3; 
        a.WEBSITE = recordItem.website; 
        a.GENDER = recordItem.gender; 
        a.ETHNICITY = recordItem.citizen; 
        a.CITIZEN = recordItem.citizen; 
        a.BIO = recordItem.bio; 
        a.TEACHING_INTERESTS = recordItem.teaching_interests; 
        a.RESEARCH_INTERESTS = recordItem.research_interests; 
        a.EXPERTISE = recordItem.expertise; 
        a.UPLOAD_PHOTO = recordItem.upload_photo; 
        a.UPLOAD_CV = recordItem.upload_cv; 

        faimdc.PCIs.InsertOnSubmit(a); 
        faimdc.SubmitChanges(); 
       } 
      } 

答えて

0

。次のようなものがあります。

var Records = from Record in userInfo.Descendants(userInfoNamespace + "Record") 
       select new PCI 
       { 
        COLLEGE = Record.Element(dmdNamespace + "IndexEntry").Attribute("entryKey").Value, 
        DEPARTMENT = Record.Element(dmdNamespace + "IndexEntry").ElementsAfterSelf(dmdNamespace + "IndexEntry").FirstOrDefault().IsEmpty ? Record.Element(dmdNamespace + "IndexEntry").ElementsAfterSelf(dmdNamespace + "IndexEntry").FirstOrDefault().Attribute("entryKey").Value : "blank", 
        … 
       } 

また、ループ内ではなく、ループの外側で一度呼び出すことをお勧めします。

関連する問題